Skip to content

Latest commit

 

History

History
175 lines (120 loc) · 6.78 KB

2_Lab2.md

File metadata and controls

175 lines (120 loc) · 6.78 KB

Lab 2 - Automate deployment for testing

Stage 1: Prepare environment for Testing

  1. Run the CloudFormation stack using the following AWS CLI command:
user:~/environment/WebAppRepo (master) $ aws cloudformation create-stack --stack-name DevopsWorkshop-Env \
--template-body https://s3.amazonaws.com/devops-workshop-0526-2051/02-aws-devops-workshop-environment-setup.template \
--capabilities CAPABILITY_IAM

Note

  • The Stack will have a VPC w/ 1 public subnet, an IGW, route tables, ACL, 2 EC2 instances. Also, the EC2 instances will be launched with a User Data script to automatically install the AWS CodeDeploy agent.

  • You can refer to this instruction to install the CodeDeploy agent for other OSs like Amazon Linux, RHEL, Ubuntu, or Windows.


Stage 2: Create CodeDeploy Application and Deployment group

  1. Run the following to create an application for CodeDeploy.
user:~/environment/WebAppRepo (master) $ aws deploy create-application --application-name DevOps-WebApp
  1. Run the following to create a deployment group and associates it with the specified application and the user's AWS account. You need to replace the service role with DeployRoleArn Value we created using roles CFN stack.
user:~/environment/WebAppRepo (master) $ echo $(aws cloudformation describe-stacks --stack-name DevopsWorkshop-roles | jq -r '.Stacks[0].Outputs[]|select(.OutputKey=="DeployRoleArn")|.OutputValue')

user:~/environment/WebAppRepo (master) $ aws deploy create-deployment-group --application-name DevOps-WebApp \
--deployment-config-name CodeDeployDefault.OneAtATime \
--deployment-group-name DevOps-WebApp-BetaGroup \
--ec2-tag-filters Key=Name,Value=DevWebApp01,Type=KEY_AND_VALUE \
--service-role-arn <<REPLACE-WITH-YOUR-CODEDEPLOY-ROLE-ARN>>

Note: We are using the tags to attach instances to the deployment group.

  1. Let us review all the changes by visiting the CodeDeploy Console.

deploy


Stage 3: Prepare application for deployment

  1. Without an AppSpec file, AWS CodeDeploy cannot map the source files in your application revision to their destinations or run scripts at various stages of the deployment.

  2. Copy the template into a text editor and save the file as appspec.yml in the WebAppRepo directory of the revision.

version: 0.0
os: linux
files:
  - source: /target/javawebdemo.war
    destination: /tmp/codedeploy-deployment-staging-area/
  - source: /scripts/configure_http_port.xsl
    destination: /tmp/codedeploy-deployment-staging-area/
hooks:
  ApplicationStop:
    - location: scripts/stop_application
      timeout: 300
  BeforeInstall:
    - location: scripts/install_dependencies
      timeout: 300
  ApplicationStart:
    - location: scripts/write_codedeploy_config.sh
    - location: scripts/start_application
      timeout: 300
  ValidateService:
    - location: scripts/basic_health_check.sh

As a sample shown below:

appspec

  1. Review the script folder in the repo for the various scripts like Start, Stop, health check etc. These scripts will be called as per the hook definition in appspec.yml file during deployment.

  2. Since we are going to deploy the application via CodeDeploy, we need to package additional files needed by CodeDeploy. Let us make change to the buildspec.yml to incorporate the changes.

version: 0.1

phases:
  install:
    commands:
      - echo Nothing to do in the install phase...
  pre_build:
    commands:
      - echo Nothing to do in the pre_build phase...
  build:
    commands:
      - echo Build started on `date`
      - mvn install
  post_build:
    commands:
      - echo Build completed on `date`
artifacts:
  files:
    - appspec.yml
    - scripts/**/*
    - target/javawebdemo.war
  1. Save the changes to buildspec.yml.

  2. Commit & push the build specification file to repository

user:~/environment/WebAppRepo/ $ git add buildspec.yml
user:~/environment/WebAppRepo/ $ git add appspec.yml
user:~/environment/WebAppRepo/ $ git commit -m "changes to build and app spec"
user:~/environment/WebAppRepo/ $ git push -u origin master

Stage 4: Deploy an application revision

  1. Run the start-build command:
user:~/environment/WebAppRepo (master) $ aws codebuild start-build --project-name devops-webapp-project
  1. Visit the CodeBuild Console to ensure build is successful. Upon successful completion of build, we should see new WebAppOutputArtifact.zip upload to the configured CodeBuild S3 Bucket.

  2. Get the eTag for the object WebAppOutputArtifact.zip uploaded to S3 bucket. You can get etag by visiting S3 console. Or, executing the following command.

user:~/environment/WebAppRepo (master) $ aws s3api head-object --bucket <<YOUR-CODEBUILD-OUTPUT-BUCKET>> \
--key WebAppOutputArtifact.zip

As a sample S3 properties console showing etag below:

etag

  1. Run the following to create a deployment. Replace <> with your S3 bucket name created in Lab 1. Also, update the eTag based on previous step.
user:~/environment/WebAppRepo (master) $ aws deploy create-deployment --application-name DevOps-WebApp \
--deployment-group-name DevOps-WebApp-BetaGroup \
--description "My very first deployment" \
--s3-location bucket=<<YOUR-CODEBUILD-OUTPUT-BUCKET>>,key=WebAppOutputArtifact.zip,bundleType=zip,eTag=<<YOUR-ETAG-VALUE>>
  1. Confirm via IAM Roles, if associated EC2 instance has appropriate permissions to read from bucket specified above. If not, you will get Access Denied at the DownloadBundle step during deployment.

  2. Verify the deployment status by visiting the CodeDeploy console.

deployment-success

  1. Check the deploy console for status. if the deployment failed, then look at the error message and correct the deployment issue.

  2. if the status of deployment is success, we should be able to view the web application deployed successfully to the EC2 server namely DevWebApp01

  3. Go to the EC2 Console, get the public DNS name of the server and open the url in a browser. You should see a sample web application.

webpage

Summary

This concludes Lab 2. In this lab, we successfully created CodeDeploy application and deployment group. We also modified buildspec.yml to include additional components needed for deployment. We also successfully completed deployment of application to test server.You can now move to the next Lab,

Lab 3 - Setup CI/CD using AWS CodePipeline