• 沒有找到結果。

Clean up resources

在文檔中 AWS CodeDeploy (頁 86-110)

c. If a message appears saying the file type could not be detected, choose .zip.

d. Leave the Deployment description box blank.

e. Expand Deployment group overrides and from Deployment configuration, choose CodeDeployDefault.OneAtATime.

f. Choose Start deployment. Information about your newly created deployment appears on the Deployments page.

g. You can check the status of the deployment, as described in Monitor and troubleshoot your deployment (p. 72).

After CodeDeploy has redeployed the site, revisit the site in your web browser to verify the colors have been changed. (You might need to refresh your browser.) If the colors have been changed, congratulations! You have successfully modified and redeployed your site!

Step 6: Clean up your WordPress application and related resources

You've now successfully made an update to the WordPress code and redeployed the site. To avoid ongoing charges for resources you created for this tutorial, you should delete:

• Any AWS CloudFormation stacks (or terminate any Amazon EC2 instances, if you created them outside of AWS CloudFormation).

• Any Amazon S3 buckets.

• The WordPress_App application in CodeDeploy.

• The AWS Systems Manager State Manager association for the CodeDeploy agent.

You can use the AWS CLI, the AWS CloudFormation, Amazon S3, Amazon EC2, and CodeDeploy consoles, or the AWS APIs to perform the cleanup.

Topics

• To clean up resources (CLI) (p. 77)

• To clean up resources (console) (p. 78)

• What's next? (p. 79)

To clean up resources (CLI)

1. If you used our AWS CloudFormation template for this tutorial, call the delete-stack command against the stack named CodeDeployDemoStack. This will terminate all accompanying Amazon EC2 instances and delete all accompanying IAM roles the stack created:

aws cloudformation delete-stack --stack-name CodeDeployDemoStack

2. To delete the Amazon S3 bucket, call the rm command with the --recursive switch against the bucket named codedeploydemobucket. This will delete the bucket and all objects in the bucket:

aws s3 rm s3://codedeploydemobucket --recursive

3. To delete the WordPress_App application, call the delete-application command. This will also delete all associated deployment group records and deployment records for the application:

aws deploy delete-application --application-name WordPress_App

Step 6: Clean up

4. To delete the Systems Manager State Manager association, call the delete-association command.

aws ssm delete-association --assocation-id association-id

You can get the association-id by calling the describe-association command.

aws ssm describe-association --name AWS-ConfigureAWSPackage --targets Key=tag:Name,Values=CodeDeployDemo

If you did not use the AWS CloudFormation stack for this tutorial, call the terminate-instances command to terminate any Amazon EC2 instances you manually created. Supply the ID of the Amazon EC2 instance to terminate:

aws ec2 terminate-instances --instance-ids instanceId

To clean up resources (console)

If you used our AWS CloudFormation template for this tutorial, delete the associated AWS CloudFormation stack.

1. Sign in to the AWS Management Console and open the AWS CloudFormation console at https://

console.aws.amazon.com/cloudformation.

2. In the Filter box, type the AWS CloudFormation stack name you created earlier (for example, CodeDeployDemoStack).

3. Select the box beside stack name. In the Actions menu, choose Delete Stack.

AWS CloudFormation deletes the stack, terminates all accompanying Amazon EC2 instances, and deletes all accompanying IAM roles.

To terminate Amazon EC2 instances you created outside of an AWS CloudFormation stack:

1. Sign in to the AWS Management Console and open the Amazon EC2 console at https://

console.aws.amazon.com/ec2/.

2. In the INSTANCES list, choose Instances.

3. In the search box, type the name of the Amazon EC2 instance you want to terminate (for example, CodeDeployDemo), and then press Enter.

4. Choose the Amazon EC2 instance name.

5. In the Actions menu, point to Instance State, and then choose Terminate. When prompted, choose Yes, Terminate.

Repeat these steps for each instance.

To delete the Amazon S3 bucket:

1. Sign in to the AWS Management Console and open the Amazon S3 console at https://

console.aws.amazon.com/s3/.

2. In the list of buckets, browse to and choose the name of the Amazon S3 bucket you created earlier (for example, codedeploydemobucket).

Tutorial: Deploy a Hello World application to a Windows Server instance

4. After the bucket is empty, you can delete the bucket. In the list of buckets, choose the row of the bucket (but not the bucket name). Choose Delete bucket, and when prompted to confirm, choose OK.

To delete the WordPress_App application from CodeDeploy:

1. Sign in to the AWS Management Console and open the CodeDeploy console at https://

console.aws.amazon.com/codedeploy.

NoteSign in with the same account or IAM user information that you used in Getting started with CodeDeploy (p. 31).

2. In the navigation pane, expand Deploy, then choose Applications.

3. In the list of applications, choose WordPress_App.

4. On the Application details page, choose Delete application.

5. When prompted, enter the name of the application to confirm you want to delete it, and then choose Delete.

To delete the Systems Manager State Manager association:

1. Open the AWS Systems Manager console at https://console.aws.amazon.com/systems-manager.

2. In the navigation pane, choose State Manager.

3. Choose the association you created and choose Delete.

What's next?

If you've arrived here, congratulations! You have successfully completed a CodeDeploy deployment, and then updated your site's code and redeployed it.

Tutorial: Deploy a "hello, world!" application with CodeDeploy (Windows Server)

In this tutorial, you deploy a single webpage to a single Windows Server Amazon EC2 instance running Internet Information Services (IIS) as its web server. This webpage displays a simple "Hello, World!"

message.

Not what you're looking for?

• To practice deploying to an Amazon Linux or Red Hat Enterprise Linux (RHEL) Amazon EC2 instance instead, see Tutorial: Deploy WordPress to an Amazon EC2 instance (Amazon Linux or Red Hat Enterprise Linux and Linux, macOS, or Unix) (p. 61).

• To practice deploying to an on-premises instance instead, see Tutorial: Deploy an application to an on-premises instance with CodeDeploy (Windows Server, Ubuntu Server, or Red Hat Enterprise Linux) (p. 94).

This tutorial's steps are presented from a Windows perspective. Although you can complete most of these steps on a local machine running Linux, macOS, or Unix, you must adapt those that cover Windows-based directory paths such as c:\temp. Also, if you want to connect to the Amazon EC2 instance, you need a client application that can connect through Remote Desktop Protocol (RDP) to the Amazon EC2 instance running Windows Server. (Windows includes an RDP connection client application by default.)

Step 1: Launch an Amazon EC2 instance

Before you start this tutorial, you must complete the prerequisites in Getting started with CodeDeploy (p. 31), including configuring your IAM user, installing or upgrading the AWS CLI, and creating an IAM instance profile and a service role.

Topics

• Step 1: Launch a Windows Server Amazon EC2 instance (p. 80)

• Step 2: Configure your source content to deploy to the Windows Server Amazon EC2 instance (p. 81)

• Step 3: Upload your "hello, world!" application to Amazon S3 (p. 83)

• Step 4: Deploy your Hello World application (p. 86)

• Step 5: Update and redeploy your "hello, world!" application (p. 90)

• Step 6: Clean up your "hello, world!" application and related resources (p. 92)

Step 1: Launch a Windows Server Amazon EC2 instance

To deploy the Hello World application with CodeDeploy, you need an Amazon EC2 instance running Windows Server.

Follow the instructions in Create an Amazon EC2 instance for CodeDeploy (p. 201). When you are ready to assign an Amazon EC2 instance tag to the instance, be sure to specify the tag key of Name and the tag value of CodeDeployDemo. (If you specify a different tag key or tag value, then the instructions in Step 4: Deploy your Hello World application (p. 86) might produce unexpected results.)

After you've launched the Amazon EC2 instance, return to this page, and continue to the next section. Do not continue on to Create an application with CodeDeploy (p. 252) as a next step.

Connect to your Amazon EC2 instance

After your Amazon EC2 instance is launched, follow these instructions to practice connecting to it.

Note

In these instructions, we assume you are running Windows and the Windows Desktop

Connection client application. For information, see Connecting to your Windows instance using RDP. You might need to adapt these instructions for other operating systems or other RDP connection client applications.

1. Sign in to the AWS Management Console and open the Amazon EC2 console at https://

console.aws.amazon.com/ec2/.

2. In the navigation pane, under Instances, choose Instances.

3. Browse to and choose your Windows Server instance in the list.

4. Choose Connect.

5. Choose Get Password, and then choose Choose File.

6. Browse to and choose the Amazon EC2 instance key pair file associated with the Windows Server Amazon EC2 instance, and then choose Open.

7. Choose Decrypt Password. Make a note of the password that is displayed. You need it in step 10.

8. Choose Download Remote Desktop File, and then open the file.

9. If you are prompted to connect even though the publisher of the remote connection can't be

Step 2: Configure your source content

11. If you are prompted to connect even though the identity of the remote computer cannot be verified, proceed.

12. After you are connected, the desktop of the Amazon EC2 instance running Windows Server is displayed.

13. You can now disconnect from the Amazon EC2 instance.

Warning

Do not stop or terminate the instance. Otherwise, CodeDeploy can't deploy to it.

Add an inbound rule that allows HTTP traffic to your Windows Server Amazon EC2 instance

The next step confirms your Amazon EC2 instance has an open HTTP port so you can see the deployed webpage on your Windows Server Amazon EC2 instance in a browser.

1. Sign in to the AWS Management Console and open the Amazon EC2 console at https://

console.aws.amazon.com/ec2/.

2. Choose Instances, and then choose your instance.

3. On the Description tab, under Security groups, choose view inbound rules.

You should see a list of rules in your security group like the following:

Security Groups associated with i-1234567890abcdef0 Ports Protocol Source launch-wizard-N 22 tcp 0.0.0.0/0 ✔

4. Under Security groups, choose the security group for your Amazon EC2 instance. It might be named launch-wizard-N. The N in the name is a number assigned to your security group when your instance was created.

Choose the Inbound tab. If the security group for your instance is configured correctly, you should see a rule with the following values:

Type: HTTP

Protocol: TCP

Port Range: 80

Source: 0.0.0.0/0

5. If you do not see a rule with these values, use the procedures in Adding Rules to a Security Group to add them to a new security rule.

Step 2: Configure your source content to deploy to the Windows Server Amazon EC2 instance

Now it's time to configure your application's source content so you have something you can deploy to the Amazon EC2 instance. For this tutorial, you'll deploy a single web page to the Amazon EC2 instance running Windows Server, which will run Internet Information Services (IIS) as its web server. This web page will display a simple "Hello, World!" message.

Topics

• Create the web page (p. 82)

• Create a script to run your application (p. 82)

• Add an application specification file (p. 83)

Step 2: Configure your source content

Create the web page

1. Create a subdirectory (subfolder) named HelloWorldApp in your c:\temp folder, and then switch to that folder.

mkdir c:\temp\HelloWorldApp cd c:\temp\HelloWorldApp

NoteYou don't have to use the location of c:\temp or the subfolder name of HelloWorldApp.

If you use a different location or subfolder name, be sure to use it throughout this tutorial.

2. Use a text editor to create a file inside of the folder. Name the file index.html.

notepad index.html

3. Add the following HTML code to the file, and then save the file.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/

html4/loose.dtd">

<html>

<head>

<title>Hello, World!</title>

<style>

body {

color: ✔ffffff;

background-color: ✔0188cc;

font-family: Arial, sans-serif;

font-size:14px;

} </style>

</head>

<body>

<div align="center"><h1>Hello, World!</h1></div>

<div align="center"><h2>You have successfully deployed an application using CodeDeploy</h2></div>

<div align="center">

<p>What to do next? Take a look through the <a href="https://aws.amazon.com/

codedeploy">CodeDeploy Documentation</a>.</p>

</div>

</body>

</html>

Create a script to run your application

Next, you will create a script that CodeDeploy will use to set up the web server on the target Amazon EC2 instance.

1. In the same subfolder where the index.html file is saved, use a text editor to create another file.

Name the file before-install.bat.

notepad before-install.bat

2. Add the following batch script code to the file, and then save the file.

Step 3: Upload your application to Amazon S3

c:\Windows\Sysnative\WindowsPowerShell\v1.0\powershell.exe -Command Install-WindowsFeature Web-Server

Add an application specification file

Next, you will add an application specification file (AppSpec file) in addition to the web page and batch script file. The AppSpec file is a YAML-formatted file used by CodeDeploy to:

• Map the source files in your application revision to their destinations on the instance.

• Specify scripts to be run on the instance during the deployment.

The AppSpec file must be named appspec.yml. It must be placed in the application source code's root folder.

1. In the same subfolder where the index.html and before-install.bat files are saved, use a text editor to create another file. Name the file appspec.yml.

notepad appspec.yml

2. Add the following YAML code to the file, and then save the file.

version: 0.0 os: windows files:

- source: \index.html

destination: c:\inetpub\wwwroot hooks:

BeforeInstall:

- location: \before-install.bat timeout: 900

CodeDeploy will use this AppSpec file to copy the index.html file in the application source code's root folder to the c:\inetpub\wwwroot folder on the target Amazon EC2 instance. During the deployment, CodeDeploy will run the before-install.bat batch script on the target Amazon EC2 instance during the BeforeInstall deployment lifecycle event. If this script takes longer than 900 seconds (15 minutes) to run, CodeDeploy will stop the deployment and mark the deployment to the Amazon EC2 instance as failed.

For more information about these settings, see the CodeDeploy AppSpec File reference (p. 376).

Important

The locations and numbers of spaces between each of the items in this file are important. If the spacing is incorrect, CodeDeploy will raise an error that may be difficult to debug. For more information, see AppSpec File spacing (p. 406).

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.

Step 3: Upload your application to Amazon S3

NoteAlthough 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 (p. 57).

Topics

• Provision an Amazon S3 bucket (p. 84)

• Prepare the application's files for the bucket (p. 86)

• Bundle the application's files into a single archive file and push the archive file (p. 86)

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 AWS 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 IAM user.

NoteBucket names must be unique across Amazon S3 for all AWS 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 AWS 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.

Topics

• To create an Amazon S3 bucket (CLI) (p. 84)

• To create an Amazon S3 bucket (console) (p. 84)

• Give permissions to the Amazon S3 bucket and your IAM user (p. 84)

To create an Amazon S3 bucket (CLI)

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

aws s3 mb s3://codedeploydemobucket

To create an Amazon S3 bucket (console)

1. Open the Amazon S3 console at https://console.aws.amazon.com/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 IAM user

Step 3: Upload your application to Amazon S3

wildcard character (*) allows AWS account 111122223333 to upload files to any directory in the Amazon S3 bucket named codedeploydemobucket:

"Resource": "arn:aws:s3:::codedeploydemobucket/*", "Principal": {

To view your AWS account ID, see Finding Your AWS 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:iam::444455556666:role/

CodeDeployDemo to download files from any directory in the Amazon S3 bucket named codedeploydemobucket:

"Resource": "arn:aws:s3:::codedeploydemobucket/*", "Principal": {

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

Your account must have permission to upload the revision to the Amazon S3 bucket. One way to specify this is through an IAM policy. The following IAM policy allows your IAM 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:s3:::codedeploydemobucket/*"

}

Step 4: Deploy your application

] }

For information about how to create and attach an IAM policy, see Working with policies.

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).

NoteYou 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

NoteIf 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

This command bundles the files from the current directory (excluding any hidden files) into a single

在文檔中 AWS CodeDeploy (頁 86-110)