Step 3: Upload your "hello, world!" application to Amazon S3 - Amazon CodeDeploy
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Step 3: Upload your "hello, world!" application to Amazon S3

Now you will prepare and upload your source content to a location from which CodeDeploy can deploy it. The following instructions show you how to provision an Amazon S3 bucket, prepare the application revision's files for the bucket, bundle the revision's files, and then push the revision to the bucket.

Note

Although it's not covered in this tutorial, you can use CodeDeploy to deploy applications from GitHub repositories to instances. For more information, see Integrating CodeDeploy with GitHub.

Provision an Amazon S3 bucket

Create a storage container or bucket in Amazon S3—or use an existing bucket. Make sure you can upload the revision to the bucket and that Amazon EC2 instances used in deployments can download the revision from the bucket.

You can use the Amazon CLI, the Amazon S3 console, or the Amazon S3 APIs to create an Amazon S3 bucket. After you create the bucket, make sure to give access permissions to the bucket and your CodeDeploy user.

Note

Bucket names must be unique across Amazon S3 for all Amazon accounts. If you aren't able to use codedeploydemobucket, try a different bucket name, such as codedeploydemobucket followed by a dash and your initials or some other unique identifier. Then be sure to substitute your bucket name for codedeploydemobucket wherever you see it throughout this tutorial.

The Amazon S3 bucket must be created in the same Amazon region in which your target Amazon EC2 instances are launched. For example, if you create the bucket in the US East (N. Virginia) Region, then your target Amazon EC2 instances must be launched in the US East (N. Virginia) Region.

To create an Amazon S3 bucket (CLI)

Call the mb command to create an Amazon S3 bucket named codedeploydemobucket:

aws s3 mb s3://codedeploydemobucket --region region

To create an Amazon S3 bucket (console)

  1. Open the Amazon S3 console at https://console.amazonaws.cn/s3/.

  2. In the Amazon S3 console, choose Create bucket.

  3. In the Bucket name box, type a name for the bucket.

  4. In the Region list, choose the target region, and then choose Create.

Give permissions to the Amazon S3 bucket and your Amazon account

You must have permissions to upload to the Amazon S3 bucket. You can specify these permissions through an Amazon S3 bucket policy. For example, in the following Amazon S3 bucket policy, using the wildcard character (*) allows Amazon account 111122223333 to upload files to any directory in the Amazon S3 bucket named codedeploydemobucket:

{ "Statement": [ { "Action": [ "s3:PutObject" ], "Effect": "Allow", "Resource": "arn:aws-cn:s3:::codedeploydemobucket/*", "Principal": { "AWS": [ "111122223333" ] } } ] }

To view your Amazon account ID, see Finding Your Amazon account ID.

Now is a good time to verify the Amazon S3 bucket will allow download requests from each participating Amazon EC2 instance. You can specify this through an Amazon S3 bucket policy. For example, in the following Amazon S3 bucket policy, using the wildcard character (*) allows any Amazon EC2 instance with an attached IAM instance profile containing the ARN arn:aws-cn:iam::444455556666:role/CodeDeployDemo to download files from any directory in the Amazon S3 bucket named codedeploydemobucket:

{ "Statement": [ { "Action": [ "s3:Get*", "s3:List*" ], "Effect": "Allow", "Resource": "arn:aws-cn:s3:::codedeploydemobucket/*", "Principal": { "AWS": [ "arn:aws-cn:iam::444455556666:role/CodeDeployDemo" ] } } ] }

For information about how to generate and attach an Amazon S3 bucket policy, see Bucket policy examples.

The CodeDeploy adminstrative user that you created in Step 1: Setting up must also have permission to upload the revision to the Amazon S3 bucket. One way to specify this is through an IAM policy, which you add to the user's permission set, or to an IAM role (which you allow the user to assume). The following IAM policy allows the user to upload revisions anywhere in the Amazon S3 bucket named codedeploydemobucket:

{ "Version":"2012-10-17", "Statement":[ { "Effect":"Allow", "Action":["s3:PutObject"], "Resource":"arn:aws-cn:s3:::codedeploydemobucket/*" } ] }

For information about how to create an IAM policy, see Creating IAM policies in the IAM User Guide. For information on adding a policy to a permission set, see Create a permission set in the Amazon IAM Identity Center User Guide.

Prepare the application's files for the bucket

Make sure the web page, the AppSpec file, and the script are organized on your development machine like this:

c:\ |-- temp\ |--HelloWorldApp\ |-- appspec.yml |-- before-install.bat |-- index.html

Bundle the application's files into a single archive file and push the archive file

Bundle the files into an archive file (known as an application revision).

Note

You may be charged for storing objects in a bucket and for transferring application revisions into and out of a bucket. For more information, see Amazon S3 pricing.

  1. On the development machine, switch to the folder where the files are stored:

    cd c:\temp\HelloWorldApp
    Note

    If you don't switch to this folder, then the file bundling will start at your current folder. For example, if your current folder is c:\temp instead of c:\temp\HelloWorldApp, the bundling will start with files and subfolders in the c:\temp folder, which may include more than the HelloWorldApp subfolder.

  2. Call the create-application command to register a new application named HelloWorld_App with CodeDeploy:

    aws deploy create-application --application-name HelloWorld_App
  3. Call the CodeDeploy push command to bundle the files together, upload the revisions to Amazon S3, and register information with CodeDeploy about the uploaded revision, all in one action.

    aws deploy push --application-name HelloWorld_App --s3-location s3://codedeploydemobucket/HelloWorld_App.zip --ignore-hidden-files

    This command bundles the files from the current directory (excluding any hidden files) into a single archive file named HelloWorld_App.zip, uploads the revision to the codedeploydemobucket bucket, and registers information with CodeDeploy about the uploaded revision.