The easiest way to create a pipeline is to use theCreate pipeline wizard in the AWS CodePipeline console.
In this tutorial, you create a two-stage pipeline that uses a versioned S3 source bucket and CodeDeploy to release a sample application.
When Amazon S3 is the source provider for your pipeline, you may zip your source file or files into a single .zip and upload the .zip to your source bucket. You may also upload a single unzipped file; however, downstream actions that expect a .zip file will fail.
As part of creating a pipeline, an S3 artifact bucket provided by the customer will be used by CodePipeline for artifacts. (This is different from the bucket used for an S3 source action.) If the S3 artifact bucket is in a different account from the account for your pipeline, make sure that the S3 artifact bucket is owned by AWS accounts that are safe and will be dependable.
After you create this simple pipeline, you add another stage and then disable and enable the transition between stages.
Many of the actions you add to your pipeline in this procedure involve AWS resources that you need to create before you create the pipeline. AWS resources for your source actions must always be created in the same AWS Region where you create your pipeline. For example, if you create your pipeline in the US East (Ohio) Region, your CodeCommit repository must be in the US East (Ohio) Region.
You can add cross-region actions when you create your pipeline. AWS resources for cross-region actions must be in the same AWS Region where you plan to execute the action. For more information, seeAdd a cross-Region action in CodePipeline.
Before you begin, you should complete the prerequisites inGetting started with CodePipeline.
You can store your source files or applications in any versioned location. In this tutorial, you create an S3 bucket for the sample application files and enable versioning on that bucket. After you have enabled versioning, you copy the sample applications to that bucket.
Sign in to the console at AWS Management Console. Open the S3 console.
ChooseCreate bucket.
InBucket name, enter a name for your bucket (for example,awscodepipeline-demobucket-example-date
).
Because all bucket names in Amazon S3 must be unique, use one of your own, not the name shown in the example. You can change the example name just by adding the date to it. Make a note of this name because you need it for the rest of this tutorial.
InRegion, choose the Region where you intend to create your pipeline, such asUS West (Oregon), and then chooseCreate bucket.
After the bucket is created, a success banner displays. ChooseGo to bucket details.
On theProperties tab, chooseVersioning. ChooseEnable versioning, and then chooseSave.
When versioning is enabled, Amazon S3 saves every version of every object in the bucket.
On thePermissions tab, leave the defaults. For more information about S3 bucket and object permissions, seeSpecifying Permissions in a Policy.
Next, download a sample and save it into a folder or directory on your local computer.
Choose one of the following. ChooseSampleApp_Windows.zip
if you want to follow the steps in this tutorial for Windows Server instances.
If you want to deploy to Amazon Linux instances using CodeDeploy, download the sample application here:SampleApp_Linux.zip.
If you want to deploy to Windows Server instances using CodeDeploy, download the sample application here:SampleApp_Windows.zip.
The sample application contains the following files for deploying with CodeDeploy:
appspec.yml
– The application specification file (AppSpec file) is aYAML-formatted file used by CodeDeploy to manage a deployment. For more information about the AppSpec file, seeCodeDeploy AppSpec File reference in theAWS CodeDeploy User Guide.
index.html
– The index file contains the home page for the deployed sample application.
LICENSE.txt
– The license file contains license information for the sample application.
Files for scripts – The sample application uses scripts to write text files to a location on your instance. One file is written for each of several CodeDeploy deployment lifecycle events as follows:
(Linux sample only)scripts
folder – The folder contains the following shell scripts to install dependencies and start and stop the sample application for the automated deployment:install_dependencies
,start_server
, andstop_server
.
(Windows sample only)before-install.bat
– This is a batch script for theBeforeInstall
deployment lifecycle event, which will run to remove old files written during previous deployments of this sample and create a location on your instance to which to write the new files.
Download the compressed (zipped) file. Do not unzip the file.
In the Amazon S3 console, for your bucket, upload the file:
ChooseUpload.
Drag and drop the file or chooseAdd files and browse for the file.
ChooseUpload.
This tutorial provides sample steps for creating Amazon EC2 Windows instances. For sample steps to create Amazon EC2 Linux instances, seeStep 3: Create an Amazon EC2 Linux instance and install the CodeDeploy agent. When prompted for the number of instances to create, specify2 instances.
In this step, you create the Windows Server Amazon EC2 instances to which you will deploy a sample application. As part of this process, you create an instance role with policies that allow install and management of the CodeDeploy agent on the instances. The CodeDeploy agent is a software package that enables an instance to be used in CodeDeploy deployments. You also attach policies that allow the instance to fetch files that the CodeDeploy agent uses to deploy your application and to allow the instance to be managed by SSM.
Open the IAM console athttps://console.aws.amazon.com/iam/).
From the console dashboard, chooseRoles.
ChooseCreate role.
UnderSelect type of trusted entity, selectAWS service. UnderChoose a use case, selectEC2, and then chooseNext: Permissions.
Search for and select the policy namedAmazonEC2RoleforAWSCodeDeploy
.
Search for and select the policy namedAmazonSSMManagedInstanceCore
. ChooseNext: Tags.
ChooseNext: Review. Enter a name for the role (for example,EC2InstanceRole
).
Make a note of your role name for the next step. You choose this role when you are creating your instance.
ChooseCreate role.
Open the Amazon EC2 console athttps://console.aws.amazon.com/ec2/.
From the side navigation, chooseInstances, and selectLaunch instances from the top of the page.
UnderName and tags, inName, enterMyCodePipelineDemo
. This assigns the instances a tagKey ofName
and a tagValue ofMyCodePipelineDemo
. Later, you create a CodeDeploy application that deploys the sample application to the instances. CodeDeploy selects instances to deploy based on the tags.
UnderApplication and OS Images (Amazon Machine Image), choose theWindows option. (This AMI is described as theMicrosoft Windows Server 2019 Base and is labeled "Free tier eligible" and can be found underQuick Start..)
UnderInstance type, choose the free tier eligiblet2.micro
type as the hardware configuration for your instance.
UnderKey pair (login), choose a key pair or create one.
You can also chooseProceed without a key pair.
For the purposes of this tutorial, you can proceed without a key pair. To use SSH to connect to your instances, create or use a key pair.
UnderNetwork settings, do the following.
InAuto-assign Public IP, make sure the status isEnable.
Next toAssign a security group, chooseCreate a new security group.
In the row forSSH, underSource type, chooseMy IP.
ChooseAdd security group, chooseHTTP, and then underSource type, chooseMy IP.
ExpandAdvanced details. InIAM instance profile, choose the IAM role you created in the previous procedure (for example,EC2InstanceRole
).
UnderSummary, underNumber of instances, enter2
..
ChooseLaunch instance.
ChooseView all instances to close the confirmation page and return to the console.
You can view the status of the launch on theInstances page. When you launch an instance, its initial state ispending
. After the instance starts, its state changes torunning
, and it receives a public DNS name. (If thePublic DNS column is not displayed, choose theShow/Hide icon, and then selectPublic DNS.)
It can take a few minutes for the instance to be ready for you to connect to it. Check that your instance has passed its status checks. You can view this information in theStatus Checks column.
In CodeDeploy, anapplication is an identifier, in the form of a name, for the code you want to deploy. CodeDeploy uses this name to ensure the correct combination of revision, deployment configuration, and deployment group are referenced during a deployment. You select the name of the CodeDeploy application you create in this step when you create your pipeline later in this tutorial.
You first create a service role for CodeDeploy to use. If you have already created a service role, you do not need to create another one.
Open the IAM console athttps://console.aws.amazon.com/iam/).
From the console dashboard, chooseRoles.
ChooseCreate role.
UnderSelect trusted entity, chooseAWS service. UnderUse case, chooseCodeDeploy. ChooseCodeDeploy from the options listed. ChooseNext. TheAWSCodeDeployRole
managed policy is already attached to the role.
ChooseNext.
Enter a name for the role (for example,CodeDeployRole
), and then chooseCreate role.
Open the CodeDeploy console athttps://console.aws.amazon.com/codedeploy.
If theApplications page does not appear, on the AWS CodeDeploy menu, chooseApplications.
ChooseCreate application.
InApplication name, enterMyDemoApplication
.
InCompute Platform, chooseEC2/On-premises.
ChooseCreate application.
On the page that displays your application, chooseCreate deployment group.
InDeployment group name, enterMyDemoDeploymentGroup
.
InService role, choose the service role you created earlier. You must use a service role that trusts AWS CodeDeploy with, at minimum, the trust and permissions described inCreate a Service Role for CodeDeploy. To get the service role ARN, seeGet the Service Role ARN (Console).
UnderDeployment type, chooseIn-place.
UnderEnvironment configuration, chooseAmazon EC2 Instances. ChooseName in theKey field, and in theValue field, enterMyCodePipelineDemo
.
You must choose the same value for theName key here that you assigned to your EC2 instances when you created them. If you tagged your instances with something other thanMyCodePipelineDemo
, be sure to use it here.
UnderAgent configuration with AWS Systems Manager, chooseNow and schedule updates. This installs the agent on the instance. The Windows instance is already configured with the SSM agent and will now be updated with the CodeDeploy agent.
UnderDeployment settings, chooseCodeDeployDefault.OneAtaTime
.
UnderLoad Balancer, make sure theEnable load balancing box is not selected. You do not need to set up a load balancer or choose a target group for this example. After you de-select the checkbox, the load balancer options do not display.
In theAdvanced section, leave the defaults.
ChooseCreate deployment group.
In this part of the tutorial, you create the pipeline. The sample runs automatically through the pipeline.
Sign in to the AWS Management Console and open the CodePipeline console athttp://console.aws.amazon.com/codesuite/codepipeline/home.
On theWelcome page,Getting started page, or thePipelines page, chooseCreate pipeline.
On theStep 1: Choose creation option page, underCreation options, choose theBuild custom pipeline option. ChooseNext.
InStep 2: Choose pipeline settings, inPipeline name, enterMyFirstPipeline
.
If you choose another name for your pipeline, be sure to use that name instead ofMyFirstPipeline
for the rest of this tutorial. After you create a pipeline, you cannot change its name. Pipeline names are subject to some limitations. For more information, seeQuotas in AWS CodePipeline.
CodePipeline provides V1 and V2 type pipelines, which differ in characteristics and price. The V2 type is the only type you can choose in the console. For more information, seepipeline types. For information about pricing for CodePipeline, seePricing.
InService role, do one of the following:
ChooseNew service role to allow CodePipeline to create a new service role in IAM.
ChooseExisting service role to use a service role already created in IAM. InRole name, choose your service role from the list.
Leave the settings underAdvanced settings at their defaults, and then chooseNext.
InStep 3: Add source stage, inSource provider, chooseAmazon S3. InBucket, enter the name of the S3 bucket you created inStep 1: Create an S3 source bucket for your application. InS3 object key, enter the object key with or without a file path, and remember to include the file extension. For example, forSampleApp_Windows.zip
, enter the sample file name as shown in this example:
SampleApp_Windows.zip
ChooseNext step.
UnderChange detection options, leave the defaults. This allows CodePipeline to use Amazon CloudWatch Events to detect changes in your source bucket.
ChooseNext.
InStep 4: Add build stage, chooseSkip build stage, and then accept the warning message by choosingSkip again. ChooseNext.
InStep 5: Add test stage, chooseSkip test stage, and then accept the warning message by choosingSkip again.
ChooseNext.
InStep 6: Add deploy stage, inDeploy provider, chooseCodeDeploy. TheRegion field defaults to the same AWS Region as your pipeline. InApplication name, enterMyDemoApplication
, or choose theRefresh button, and then choose the application name from the list. InDeployment group, enterMyDemoDeploymentGroup
, or choose it from the list, and then chooseNext.
The name Deploy is the name given by default to the stage created in theStep 4: Add deploy stage step, just as Source is the name given to the first stage of the pipeline.
InStep 7: Review, review the information, and then chooseCreate pipeline.
The pipeline starts to run. You can view progress and success and failure messages as the CodePipeline sample deploys a webpage to each of the Amazon EC2 instances in the CodeDeploy deployment.
Congratulations! You just created a simple pipeline in CodePipeline. The pipeline has two stages:
A source stage namedSource, which detects changes in the versioned sample application stored in the S3 bucket and pulls those changes into the pipeline.
ADeploy stage that deploys those changes to EC2 instances with CodeDeploy.
Now, verify the results.
View the initial progress of the pipeline. The status of each stage changes fromNo executions yet toIn Progress, and then to eitherSucceeded orFailed. The pipeline should complete the first run within a few minutes.
AfterSucceeded is displayed for the action status, in the status area for theDeploy stage, chooseDetails. This opens the CodeDeploy console.
In theDeployment group tab, underDeployment lifecycle events, choose an instance ID. This opens the EC2 console.
On theDescription tab, inPublic DNS, copy the address, and then paste it into the address bar of your web browser. View the index page for the sample application you uploaded to your S3 bucket.
The web page displays for the sample application you uploaded to your S3 bucket.
For more information about stages, actions, and how pipelines work, seeCodePipeline concepts.
Now add another stage in the pipeline to deploy from staging servers to production servers using CodeDeploy. First, you create another deployment group in the CodePipelineDemoApplication in CodeDeploy. Then you add a stage that includes an action that uses this deployment group. To add another stage, you use the CodePipeline console or the AWS CLI to retrieve and manually edit the structure of the pipeline in a JSON file, and then run theupdate-pipeline command to update the pipeline with your changes.
In this part of the tutorial, you create a second deployment group, but deploy to the same Amazon EC2 instances as before. This is for demonstration purposes only. It is purposely designed to fail to show you how errors are displayed in CodePipeline.
Open the CodeDeploy console athttps://console.aws.amazon.com/codedeploy.
ChooseApplications, and in the list of applications, chooseMyDemoApplication
.
Choose theDeployment groups tab, and then chooseCreate deployment group.
On theCreate deployment group page, inDeployment group name, enter a name for the second deployment group (for example,CodePipelineProductionFleet
).
InService Role, choose the same CodeDeploy service role you used for the initial deployment (not the CodePipeline service role).
UnderDeployment type, chooseIn-place.
UnderEnvironment configuration, chooseAmazon EC2 Instances. ChooseName in theKey box, and in theValue box, chooseMyCodePipelineDemo
from the list. Leave the default configuration forDeployment settings.
UnderDeployment configuration, chooseCodeDeployDefault.OneAtaTime
.
UnderLoad Balancer, clearEnable load balancing.
ChooseCreate deployment group.
Now that you have another deployment group, you can add a stage that uses this deployment group to deploy to the same EC2 instances you used earlier. You can use the CodePipeline console or the AWS CLI to add this stage.
You can use the CodePipeline console to add a new stage that uses the new deployment group. Because this deployment group is deploying to the EC2 instances you've already used, the deploy action in this stage fails.
Sign in to the AWS Management Console and open the CodePipeline console athttp://console.aws.amazon.com/codesuite/codepipeline/home.
InName, choose the name of the pipeline you created, MyFirstPipeline.
On the pipeline details page, chooseEdit.
On theEdit page, choose+ Add stage to add a stage immediately after the Deploy stage.
InAdd stage, inStage name, enterProduction
. ChooseAdd stage.
In the new stage, choose+ Add action group.
InEdit action, inAction name, enterDeploy-Second-Deployment
. InAction provider, underDeploy, chooseCodeDeploy.
In the CodeDeploy section, inApplication name, chooseMyDemoApplication
from the drop-down list, as you did when you created the pipeline. InDeployment group, choose the deployment group you just created,CodePipelineProductionFleet
. InInput artifacts, choose the input artifact from the source action. ChooseSave.
On theEdit page, chooseSave. InSave pipeline changes, chooseSave.
Although the new stage has been added to your pipeline, a status ofNo executions yet is displayed because no changes have triggered another run of the pipeline. You must manually rerun the last revision to see how the edited pipeline runs. On the pipeline details page, chooseRelease change, and then chooseRelease when prompted. This runs the most recent revision available in each source location specified in a source action through the pipeline.
Alternatively, to use the AWS CLI to rerun the pipeline, from a terminal on your local Linux, macOS, or Unix machine, or a command prompt on your local Windows machine, run thestart-pipeline-execution command, specifying the name of the pipeline. This runs the application in your source bucket through the pipeline for a second time.
aws codepipeline start-pipeline-execution --name MyFirstPipeline
This command returns apipelineExecutionId
object.
Return to the CodePipeline console and in the list of pipelines, chooseMyFirstPipeline to open the view page.
The pipeline shows three stages and the state of the artifact running through those three stages. It might take up to five minutes for the pipeline to run through all stages. You see the deployment succeeds on the first two stages, just as before, but theProduction stage shows theDeploy-Second-Deployment action failed.
In theDeploy-Second-Deployment action, chooseDetails. You are redirected to the page for the CodeDeploy deployment. In this case, the failure is the result of the first instance group deploying to all of the EC2 instances, leaving no instances for the second deployment group.
This failure is by design, to demonstrate what happens when there is a failure in a pipeline stage.
Although using the AWS CLI to add a stage to your pipeline is more complex than using the console, it provides more visibility into the structure of the pipeline.
Open a terminal session on your local Linux, macOS, or Unix machine, or a command prompt on your local Windows machine, and run theget-pipeline command to display the structure of the pipeline you just created. ForMyFirstPipeline
, you would type the following command:
aws codepipeline get-pipeline --name "MyFirstPipeline
"
This command returns the structure of MyFirstPipeline. The first part of the output should look similar to the following:
{ "pipeline":{ "roleArn": "arn:aws:iam::80398EXAMPLE:role/AWS-CodePipeline-Service", "stages": [ ...
The final part of the output includes the pipeline metadata and should look similar to the following:
... ], "artifactStore":{ "type": "S3" "location": "amzn-s3-demo-bucket", }, "name": "MyFirstPipeline", "version": 4 }, "metadata":{ "pipelineArn": "arn:aws:codepipeline:us-east-2:80398EXAMPLE:MyFirstPipeline", "updated": 1501626591.112, "created": 1501626591.112 }}
Copy and paste this structure into a plain-text editor, and save the file aspipeline.json
. For convenience, save this file in the same directory where you run theaws codepipeline commands.
Copy theDeploy stage section and paste it after the first two stages. Because it is a deploy stage, just like theDeploy stage, you use it as a template for the third stage.
Change the name of the stage and the deployment group details.
The following example shows the JSON you add to the pipeline.json file after theDeploy stage. Edit the emphasized elements with new values. Remember to include a comma to separate theDeploy andProduction stage definitions.
,{ "name": "Production
", "actions": [{ "inputArtifacts": [{ "name": "MyApp" } ], "name": "Deploy-Second-Deployment
", "actionTypeId":{ "category": "Deploy", "owner": "AWS", "version": "1", "provider": "CodeDeploy" }, "outputArtifacts": [], "configuration":{ "ApplicationName": "CodePipelineDemoApplication", "DeploymentGroupName": "CodePipelineProductionFleet
" }, "runOrder": 1 } ]}
If you are working with the pipeline structure retrieved using theget-pipeline command, you must remove themetadata
lines from the JSON file. Otherwise, theupdate-pipeline command cannot use it. Remove the"metadata":{ }
lines and the"created"
,"pipelineARN"
, and"updated"
fields.
For example, remove the following lines from the structure:
"metadata":{ "pipelineArn": "arn:aws:codepipeline:region
:account-ID
:pipeline-name
", "created": "date
", "updated": "date
" }
Save the file.
Run theupdate-pipeline command, specifying the pipeline JSON file, similar to the following:
aws codepipeline update-pipeline --cli-input-json file://pipeline.json
This command returns the entire structure of the updated pipeline.
Be sure to includefile://
before the file name. It is required in this command.
Run thestart-pipeline-execution command, specifying the name of the pipeline. This runs the application in your source bucket through the pipeline for a second time.
aws codepipeline start-pipeline-execution --name MyFirstPipeline
This command returns apipelineExecutionId
object.
Open the CodePipeline console and chooseMyFirstPipeline from the list of pipelines.
The pipeline shows three stages and the state of the artifact running through those three stages. It might take up to five minutes for the pipeline to run through all stages. Although the deployment succeeds on the first two stages, just as before, theProduction stage shows that theDeploy-Second-Deployment action failed.
In theDeploy-Second-Deployment action, chooseDetails to see details of the failure. You are redirected to the details page for the CodeDeploy deployment. In this case, the failure is the result of the first instance group deploying to all of the EC2 instances, leaving no instances for the second deployment group.
This failure is by design, to demonstrate what happens when there is a failure in a pipeline stage.
You can enable or disable the transition between stages in a pipeline. Disabling the transition between stages allows you to manually control transitions between one stage and another. For example, you might want to run the first two stages of a pipeline, but disable transitions to the third stage until you are ready to deploy to production, or while you troubleshoot a problem or failure with that stage.
Open the CodePipeline console and chooseMyFirstPipeline from the list of pipelines.
On the details page for the pipeline, choose theDisable transition button between the second stage (Deploy) and the third stage that you added in the previous section (Production).
InDisable transition, enter a reason for disabling the transition between the stages, and then chooseDisable.
The arrow between stages displays an icon and color change, and theEnable transition button.
Upload your sample again to the S3 bucket. Because the bucket is versioned, this change starts the pipeline.
Return to the details page for your pipeline and watch the status of the stages. The pipeline view changes to show progress and success on the first two stages, but no changes occur on the third stage. This process might take a few minutes.
Enable the transition by choosing theEnable transition button between the two stages. In theEnable transition dialog box, chooseEnable. The stage starts running in a few minutes and attempts to process the artifact that has already been run through the first two stages of the pipeline.
If you want this third stage to succeed, edit the CodePipelineProductionFleet deployment group before you enable the transition, and specify a different set of EC2 instances where the application is deployed. For more information about how to do this, seeChange deployment group settings. If you create more EC2 instances, you might incur additional costs.
You can use some of the resources you created in this tutorial for theTutorial: Create a four-stage pipeline. For example, you can reuse the CodeDeploy application and deployment. You can configure a build action with a provider such as CodeBuild, which is a fully managed build service in the cloud. You can also configure a build action that uses a provider with a build server or system, such as Jenkins.
However, after you complete this and any other tutorials, you should delete the pipeline and the resources it uses, so that you are not charged for the continued use of those resources. First, delete the pipeline, then the CodeDeploy application and its associated Amazon EC2 instances, and finally, the S3 bucket.
To clean up your CodePipeline resources, follow the instructions inDelete a pipeline in AWS CodePipeline.
To clean up your CodeDeploy resources, follow the instructions inTo clean up resources (console).
To delete the S3 bucket, follow the instructions inDeleting or emptying a bucket. If you do not intend to create more pipelines, delete the S3 bucket created for storing your pipeline artifacts. For more information about this bucket, seeCodePipeline concepts.