Deploy to AWS with CodeDeploy

These options provide you with a simplified way of deploying to AWS. This approach requires less maintenance since the pipeline is maintained on your behalf. 

Before you begin

To deploy your application with AWS CodeDeploy you’ll need to have:

  • An IAM user is configured with sufficient permissions to allow the pipe to perform a deployment to your application and upload artifacts to the S3 bucket.
  • You have configured a CodeDeploy Application and Deployment Group. Here is a simple tutorial from AWS: Deploy Code to a Virtual Machine
  • Set up an S3 bucket where deployment artifacts will be copied.

Steps

  1. Clone the AWS CodePloy pipe example repository.

  2. Add your AWS credentials to Bitbucket Pipelines. In your repo go to Settings, under Pipelines, select Repository variables and add the following two variables:

    Basic usage variables 

    • AWS_ACCESS_KEY_ID (*): Your AWS access key.

    • AWS_SECRET_ACCESS_KEY (*): Your AWS secret access key. Make sure that you save it as a secured variable.

    • AWS_DEFAULT_REGION (*):  The AWS region code (us-east-1, us-west-2, etc.) of the region containing the AWS resource(s). For more information, The AWS region code (us-east-1, us-west-2, etc.) of the region containing the AWS resource(s). For more information, see Regions and Endpoints.

    • APPLICATION_NAME (*):Application name.

    • COMMAND (*): Mode of operation: upload or deploy. See the Details section to understand how each mode works.

    • DEBUGTurn on extra debug information. Default: false.

    • (*) = required variable.

Advanced usage

For advanced use cases It's recommended the build once and deploy many approach. If you have multiple environments, use COMMAND variable to separate your CI/CD workflow into different operations/pipes:

    • COMMAND: 'upload-only'
    • COMMAND: 'deploy-only'

Upload-only variables

If COMMAND is set to upload:

        • ZIP_FILE (*): The application source bundle to deploy (zip, jar, war).

        • S3_BUCKET (*)Override the S3 bucket that the application zip is uploaded to and deployed from. The default follows the convention <application_name>-codedeploy-deployment

        • VERSION_LABEL (*)Override the name of the application revision in S3. The default follows the convention <application_name>-<build_number>-<commit>.

      1. (*) = required variable.


        script:
          - pipe: atlassian/aws-code-deploy:0.2.7
            variables:
              AWS_DEFAULT_REGION: 'ap-southeast-2'
              AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
              AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
              COMMAND: 'upload'
              APPLICATION_NAME: 'my-application'
              ZIP_FILE: 'myapp.zip'
              S3_BUCKET: 'my-bucket'
              VERSION_LABEL: 'my-app-1.0.0'

      Deploy-only variables


If COMMAND is set to deploy:

      • DEPLOYMENT_GROUP (*): Name of the Deployment Group.

      • S3_BUCKET: Override the S3 bucket that the application zip is uploaded to and deployed from. The default follows the convention <application_name>-codedeploy-deployment

      • VERSION_LABEL: Override the name of the application revision in S3. The default follows the convention <application_name>-<build_number>-<commit>.

      • WAIT: Wait for deployment to complete. Default: false.

      • FILE_EXISTS_BEHAVIOR: Action to take if files already exist in the deployment target location (defined in the AppSpec file). Allowed values: OVERWRITEDISALLOWRETAIN, default: DISALLOW.

      • IGNORE_APPLICATION_STOP_FAILURES: Ignore any errors thrown when trying to stop the previous version of the deployed application. Default: false.

      • EXTRA_ARGS: Additional args to pass to aws deploy create-deployment.

        (*) = required variable.

    1. Examples:
    2. Start a deployment and wait for it to finish. The application revision my-application-<build-number>-<commit> from the S3 bucket my-application-codedeploy-deployment will be deployed.
    3. script:
        - pipe: atlassian/aws-code-deploy:0.2.7
          variables:
            AWS_DEFAULT_REGION: 'ap-southeast-2'
            AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
            AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
            COMMAND: 'deploy'
            APPLICATION_NAME: 'my-application'
            DEPLOYMENT_GROUP: 'my-deployment-group'
            WAIT: 'true'


      Start a deployment, referencing an application revision uploaded to a custom location in S3. The application revision my-app-1.0.0 from the S3 bucket my-bucketwill be deployed.

      script:
        - pipe: atlassian/aws-code-deploy:0.2.7
          variables:
            AWS_DEFAULT_REGION: 'ap-southeast-2'
            AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
            AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
            COMMAND: 'deploy'
            APPLICATION_NAME: 'my-application'
            DEPLOYMENT_GROUP: 'my-deployment-group'
            WAIT: 'true'
            S3_BUCKET: 'my-bucket'
            VERSION_LABEL: 'my-app-1.0.0'

      Start a deployment, and ignore any application stop failures, and force overwrite previous deployment files.

      script:
        - pipe: atlassian/aws-code-deploy:0.2.7
          variables:
            AWS_DEFAULT_REGION: 'ap-southeast-2'
            AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
            AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
            COMMAND: 'deploy'
            APPLICATION_NAME: 'my-application'
            DEPLOYMENT_GROUP: 'my-deployment-group'
            WAIT: 'true'
            IGNORE_APPLICATION_STOP_FAILURES: 'true'
            FILE_EXISTS_BEHAVIOR: 'OVERWRITE'

      Note

      When you use the deploy mode with the default VERSION_LABEL, the pipe generates a new version label based on the build number and commit hash. You need to make sure to also run the pipe with the upload mode within the same pipeline so the corresponding version is preset in S3. If you don't run the upload part of the pipe in the same pipeline, you should use explicit VERSION_LABEL, for instance, use a versioning scheme that is decoupled from the build number.


Last modified on Sep 18, 2019

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.