Utilizing AWS CodeDeploy and AWS CodePipeline to Deploy Purposes to Amazon Lightsail

This submit is contributed by Mike Coleman | Developer Advocate for Lightsail | Twitter: @mikegcoleman

Amazon Lightsail is the best solution to get began within the cloud, permitting you to get your utility operating by yourself digital server in a matter of minutes. However, what do you do if you wish to replace that operating utility?

With a view to automate the method of each deploying and updating software program many builders are turning to automated workflows. AWS has a full complement of instruments that let you construct deployment pipelines to cowl a big selection of use instances.

This weblog submit supplies steering on learn how to configure Lightsail to work with AWS CodePipeline and AWS CodeDeploy to robotically deploy (or replace) an utility each time you push a change to GitHub. Despite the fact that this tutorial supplies detailed, step-by-step directions, you should still need to learn among the docs (CodeDeploy and CodePipeline) in case you’re unfamiliar with deployment pipelines.

After finishing this walkthrough you’ll be in your solution to implementing extra complicated pipelines that may embrace construct and take a look at steps.

 

You want the next to finish this walkthrough:

  • A GithHub account wherein to fork the demo code
  • git put in in your native machine, and a primary understanding on learn how to use it
  • An AWS account with enough privileges to create AWS Identification and Entry (IAM) customers, insurance policies, and repair roles in addition to to create sources with the next providers: Amazon S3, CodePipeline, CodeDeploy and Lightsail
  • The AWS CLI put in and configured in your native machine

 

As you undergo this tutorial, you’ll must pay attention to a number of key values. Open up a textual content editor of your selection, and replica and paste the template beneath into a brand new doc. As you undergo the information, when instructed, copy the values into your textual content doc

S3 Bucket Identify:

Entry Key ID:

Secret Key:

IAM Person ARN:

Notice: Each the Entry key ID and Secret entry key needs to be protected in the identical method you shield any delicate username / password pair.

 

The remainder of this weblog guides you thru the method of establishing your deployment pipeline. You begin by making a service function for CodeDeploy, an Amazon S3 bucket, and an IAM person. After deploying these providers, you create a Lightsail example. You additionally set up and configure the CodeDeploy agent, in addition to registering the example with CodeDeploy. Lastly, you create an utility in CodeDeploy, and configure CodePipeline to kick off a brand new deployment everytime you push modifications to GitHub.

 

Table of Contents

Create a service function

In AWS a service function is a task that an AWS service assumes to carry out actions in your behalf. The insurance policies that you just connect to the service function decide which AWS sources the service can entry and what it might do with these sources. Under you utilize AWS IAM to create a service function for CodeDeploy that has the mandatory permissions to work with Lightsail.

  1. Check in to the AWS Administration Console and open the IAM console at https://console.aws.amazon.com/iam/.
  2. Within the navigation pane, select Roles, after which select Create function.                                iam role
  3. On the Create function web page, select AWS service, and from the Select the service that can use this function record, select CodeDeploy.                                  iam role for service
  4. Close to the underside of the display screen below Choose your use case, select CodeDeploy and click on Subsequent: Permissions.
  5. On the Connected permissions coverage web page, the default permission coverage (AWSCodeDeployRule) is displayed. You may click on on the coverage identify in case you’d prefer to evaluation the main points of the coverage.Click on Subsequent: Tags, after which click on Subsequent: Overview.
  6. On the Create Position web page, in Position identify, enter a reputation for the service function (for instance, CodeDeployServiceRole).role name
  7. Click on Create function.

 

Create an S3 bucket

On this part, you create an Amazon S3 bucket to retailer the deployment artifact created by CodeDeploy. This artifact is a compressed file containing your supply code information, and any scripts that must run as a part of the set up or replace course of.

  1. Check in to the AWS Administration Console and open the S3 console, and click on Create Bucket. create s3 bucket
  2. Enter a reputation below Bucket identify. The identify have to be distinctive throughout all of S3.
    You should definitely copy the S3 bucket identify into your textual content doc.
  3. Guarantee Block all public entry is checked.                             name s3 bucket
  4. Click on Create bucket.

 

Create IAM coverage

An IAM coverage is a algorithm that, when hooked up to an id or useful resource, defines their permissions. For this use case, you need a coverage that solely permits AWS CodeDeploy agent to learn the S3 bucket you simply created. Within the subsequent set of steps, you outline the coverage. Then within the subsequent part you apply that coverage to an IAM person. That person in the end is related to the CodeDeploy agent operating in your Lightsail example.

  1. Check in to the AWS Administration Console and open the IAM console.
  2. Within the navigation pane, select Insurance policies, after which select Create coverage. create iam policy
  3. Click on on the JSON tab.                                                                                            JSON tab

Erase the content material within the editor window, and paste within the code from beneath.

NOTE: You should definitely change <S3 Bucket Identify> with the identify of the S3 bucket you created within the earlier step.

{

  "Version": "2012-10-17",

  "Statement": [

    {

      "Effect": "Allow",

      "Action": [

        "s3:Get*",

        "s3:List*"

      ],

      "Resource": [

        "arn:aws:s3:::/*"

      ]

    }

  ]

}

JSON editor revised

  1. Click on Overview coverage.
  2. Enter CodeDeployS3BucketPolicy for the coverage identify.
  3. Click on Create coverage.

Create an IAM person

Since you can’t assign an IAM function to a Lightsail example, it’s essential create your IAM person with the suitable permissions. On this case the person will want to have the ability to record the contents of the S3 bucket you simply created.

  1. Keep within the IAM console.
  2. Within the navigation pane, select Customers, after which select Add person.                                      IAM user creation
  3. Enter LightSailCodeDeployUser for the Person identify and click on Programmatic entry below Choose AWS entry sort (you utilize programmatic entry since this person account won’t ever must log into the console). Click on Subsequent: permissions.        set user name
  4. Click on Connect present polices immediately. Enter CodeDeployS3BucketPolicy within the search field, and examine the field subsequent to the CodeDeployS3BucketPolicy coverage.                                        attach policies to the S3 bucket
  5. Click on Subsequent: Tags. Click on Subsequent: Overview. Click on Create person.
  6. Copy the Entry key ID and Secret entry key into your textual content doc. You will want to click on Present to show the key entry key. Notice: If you don’t copy these values now, you can not return and retrieve them from the console. You will want to create a brand new set of credentials

.access key and secret access key

       7. Click on Shut.

8. Click on on the person you simply and replica the Person ARN into your doc.  USR ARN

At this level you created an S3 bucket the place CodeDeploy can retailer your construct artifact, in addition to the IAM elements you want (a service function, IAM Coverage, and an IAM person) to configure the CodeDeploy agent. Within the subsequent step, you really deploy a Lightsail example with the CodeDeploy agent, and you then register that example with CodeDeploy

Create a Lightsail example and set up the CodeDeploy agent

On this part you create the Lightsail example the place you need your code to run. To ensure that the example to work with CodeDeploy it’s essential to set up the CodeDeploy agent. The agent set up is finished by offering a startup script that runs when the example is first created.

  1. Log in to the AWS Administration Console, and navigate to the Lightsail house web page.
  2. Click on Create example.
  3. Lightsail instance location
  4. Be sure that you’re creating your example within the appropriate AWS Area.
  5. Beneath Decide your example picture click on on Linux/Unix. Click on on OS Solely. Choose Amazon Linux.instance image lightsail
  6. Scroll down and click on + Add launch script. Within the code beneath paste in your Entry key ID, Secret entry key, and IAM Person ARN out of your textual content doc. Additionally change <Desired Area> with the Area that you just deployed example into (e.g. us-west-2).

After you edit the code beneath, paste it into the launch script edit window. The configuration beneath permits the CodeDeploy agent run with the permissions you assigned to the IAM person earlier. These permissions permit the CodeDeploy agent to obtain the deployment artifact created by the CodeDeploy service from the S3 bucket the place it will likely be saved. Moreover, the agent will use the data within the artifact to deploy or replace your utility.

mkdir /and so on/codedeploy-agent/

mkdir /and so on/codedeploy-agent/conf

cat <> /and so on/codedeploy-agent/conf/codedeploy.onpremises.yml

---

aws_access_key_id:

aws_secret_access_key:

iam_user_arn:

location:

EOT

wget https://aws-codedeploy-us-west-2.s3.us-west-2.amazonaws.com/newest/set up

chmod +x ./set up

sudo ./set up auto

     6. Enter codedeploy for the example identify below Determine your example. Click on Create Occasion.

Confirm the CodeDeploy agent

Wait about 5-10 minutes for the example besides up and run the startup script.

On this part you confirm that the CodeDeploy agent is up and operating, register your example with CodeDeploy, and, lastly, tag the example.

Notice: You can be utilizing the command line for each your Lightsail example and in your native machine. Pay cautious consideration to the directions to make sure you’re issuing the instructions on the appropriate command line.

  1. Begin an SSH session by clicking on the terminal icon subsequent to the identify of your examplessh session for instance
  2. On the command line of the Lightsail terminal session enter the command beneath to confirm the CodeDeploy agent is operating.

sudo service codedeploy-agent standing

You need to see a response just like the one beneath (the PID will likely be completely different):

The AWS CodeDeploy agent is operating as PID 2783

      3. Enter the command beneath utilizing the AWS CLI in a terminal session on your native machine to register your Lightsail example with CodeDeploy.

NOTE: Change <IAM Person ARN> with the worth in your doc and the <Desired Area> with the suitable Area.
NOTE: For those who didn’t identify your Lightsail example codedeploy you have to to regulate the –instance-name parameter accordingly.
NOTE: The command doesn’t present any output

aws deploy register-on-premises-instance --instance-name codedeploy --iam-user-arn --region

  1. Enter the next command utilizing the AWS CLI in a terminal session on your native machine to tag your Lightsail example in CodeDeploy. The tag will likely be utilized by CodeDeploy to know the place to put in your code.
    NOTE: For those who didn’t identify your Lightsail example codedeploy you have to to regulate the –instance-name parameter accordingly.
    NOTE: Change <Desired Area> with the suitable Area.
    NOTE: The command doesn’t present any output

aws deploy add-tags-to-on-premises-instances --instance-names codedeploy --tags Key=Identify,Worth=CodeDeployLightsailDemo --region

      5. Enter the command beneath utilizing the AWS CLI in a terminal session on your native machine to confirm your machine was efficiently registered:
          NOTE: Change <Desired Area> with the suitable Area.

aws deploy list-on-premises-instances --region

You need to see output just like:

{
"instanceNames": [
     "codedeploy"
  ]
}

At this level you are actually able to setup your precise code deployment utilizing CodePipeline and CodeDeploy.

Setup the appliance in CodeDeploy

  1. Navigate to the CodeDeploy console, be sure you’re within the appropriate Area, and click on Create utility.codedeploy create application
  2. Enter CodeDeployLightsailDemo for the Utility identify and choose EC2/On-premises below Compute platform. Click on Create utility.
  3. In the Deployment teams part Click on Create deployment group.
  4. Enter CodeDeployLightsailDemoDeploymentGroup for the Deployment group identify.
  5. Click on within the textual content field for Enter a service function and choose the service function you created earlier (CodeDeployServiceRole)
  6. Beneath Atmosphere configuration examine the field for On-premises occasions. Beneath Key enter Identify and below Worth enter. environment configuration
  7. Beneath Load balancer uncheck Allow load balancing.
  8. Click on Create deployment group.

 

Fork the GitHub Repo

On this part, you join your GitHub account to CodePipeline in order that everytime you push a change to GitHub your new code will robotically be deployed. For this tutorial, I positioned a small demo utility in a GitHub repository. These subsequent steps information you thru forking that code into your individual GitHub account.

  1. Signal into GithHub.
  2. Navigate to the demo repository: http://github.com/mikegcoleman/codedeploygithubdemo
  3. To the appropriate of the repository identify on the high click on the Forkfork github
  4. Click on on the account that you just need to fork the repository into.
    After a number of seconds the fork course of completes, and you’re redirected to the brand new repo in your account.

Setup CodePipeline

Because the identify implies, CodePipeline means that you can create an automatic set of steps on your utility deployment. As an illustration, construct and take a look at processes that should occur earlier than a last deployment step. On this instance you’re going to construct a quite simple pipeline that can redeploy your utility when a change is pushed to the related GitHub repository.

  1. Navigate to the CodePipeline console, make sure you’re within the appropriate Area, and click on Create pipeline.
  2. Enter CodeDeployLightsailDemoPipeline for the Pipeline identify.
  3. Click on on Superior Settings. Beneath Artifact retailer click on the radio button subsequent to Customized Location. Click on into the Bucket textual content field and choose the S3 bucket you created earlier. Click on Subsequent.codepipeline advanced settings
  4. From the Supply supplier drop down select Click on Hook up with GitHub and comply with any prompts to authorize CodePipeline to entry your GitHub account.
    1. Notice: For those who’ve related GitHub beforehand there is not going to be any extra prompts.
  5. Click on within the Repository textual content field and choose the repository you forked earlier. The identify needs to be /codedeploygithubdemo.
  6. Click on within the Department field and select Click on Subsequent. Since there isn’t a construct stage click on Skip construct stage and make sure by clicking Skip.
  7. Select AWS CodeDeploy from the Deploy supplier Guarantee the suitable Area is chosen. Select CodeDeployLightsailDemo from the Utility identify record. Select CodeDeployLightsailDemoDeploymentGroup from the Deployment group record.
    1. Click on Subsequent.
    2. Click on Create pipeline.codedeploy configuration
  8. You’ll be taken to the main points web page on your pipeline, and might watch the standing of the pipeline replace. As soon as the Deploy step has a standing of succeeded be happy to maneuver on to the subsequent part.                                                                                           source for code

 

Take a look at and Replace the Utility

On this last part you to confirm the appliance deployed, and you then’ll make an replace to the appliance to kick off a brand new deployment. Lastly, you’ll confirm that the replace was efficiently pushed to your server.

  1. Navigate in your net browser to the IP handle of your Lightsail example. You will discover the IP handle on the cardboard on your example on the Lightsail house web page. You need to see a easy webpage displayed.
    ip address
  2. Transfer to the command line of your native machine, and clone the GitHub repository, being positive to insert your GitHub username.NOTE: If you’re utilizing SSH to authenticate to GitHub alter the GitHub command accordinglygit clone https://github.com//codedeploygithubdemoYou need to see output just like the next:

    Cloning into 'codedeploygithubdemo'...
    distant: Enumerating objects: 49, finished.
    distant: Counting objects: 100% (49/49), finished.
    distant: Compressing objects: 100% (33/33), finished.
    distant: Whole 49 (delta 25), reused 36 (delta 12), pack-reused 0
    Unpacking objects: 100% (49/49), finished.

  3. Develop into the listing with the web site code:cd codedeploygithubdemo
  4. Utilizing an editor of your selection edit the html file by altering the background colour to purple:background-color: purple;
  5. Push the modifications to GitHub by issuing every of the next instructions one by one:git add index.html
    git commit -m “new background color”
    git push origin grasp
  6. Navigate again to the CodePipeline console and click on on the identify of your pipeline. You need to see that the change from GitHub has been picked up and the pipeline is deploying your web site. As soon as the pipeline has efficiently accomplished transfer to the subsequent step.
  7. Reload to demo web site to see that the background colour has modified.

Congratulations on ending this tutorial. You need to now have an understanding of learn how to automate the deployment and updating of your utility operating on Lightsail utilizing CodeDeploy and CodePipeline. As a subsequent step, you may need to try to deploy a extra complicated utility to Lightsail, together with including a construct step. You will discover data on how to try this in AWS CodeBuild documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *