|
| 1 | ++++ |
| 2 | +title = "Build Stage" |
| 3 | +date = 2019-11-01T15:26:09-07:00 |
| 4 | +weight = 20 |
| 5 | ++++ |
| 6 | + |
| 7 | +The **Build Stage** is where your Serverless application gets built and packaged by SAM. We are going to use AWS CodeBuild as the Build provider for our pipeline. It is worth mentioning that CodePipeline also supports other providers like Jenkins, TeamCity or CloudBees. |
| 8 | + |
| 9 | +### Why AWS Code Build? |
| 10 | + |
| 11 | +AWS CodeBuild is a great option because you only pay for the time where your build is running, which makes it very cost effective compared to running a dedicated build server 24 hours a day when you really only build during office hours. It is also container-based which means that you can bring your own Docker container image where your build runs, or [use a managed image](https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-available.html) provided by CodeBuild. |
| 12 | + |
| 13 | +### Add the build stage |
| 14 | + |
| 15 | +Let's go ahead and add a Build stage to you pipeline-stack.ts: |
| 16 | + |
| 17 | +```js |
| 18 | +// Declare build output as artifacts |
| 19 | +const buildOutput = new codepipeline.Artifact(); |
| 20 | + |
| 21 | +// Declare a new CodeBuild project |
| 22 | +const buildProject = new codebuild.PipelineProject(this, 'Build', { |
| 23 | + environment: { buildImage: codebuild.LinuxBuildImage.AMAZON_LINUX_2_2 }, |
| 24 | + environmentVariables: { |
| 25 | + 'PACKAGE_BUCKET': { |
| 26 | + value: artifactsBucket.bucketName |
| 27 | + } |
| 28 | + } |
| 29 | +}); |
| 30 | + |
| 31 | +// Add the build stage to our pipeline |
| 32 | +pipeline.addStage({ |
| 33 | + stageName: 'Build', |
| 34 | + actions: [ |
| 35 | + new codepipeline_actions.CodeBuildAction({ |
| 36 | + actionName: 'Build', |
| 37 | + project: buildProject, |
| 38 | + input: sourceOutput, |
| 39 | + outputs: [buildOutput], |
| 40 | + }), |
| 41 | + ], |
| 42 | +}); |
| 43 | +``` |
| 44 | + |
| 45 | +{{%expand "Click here to see how the entire file should look like" %}} |
| 46 | + |
| 47 | +The highlighted code is the new addition: |
| 48 | + |
| 49 | +{{< highlight js "hl_lines=44-68" >}} |
| 50 | +// lib/pipeline-stack.ts |
| 51 | + |
| 52 | +import * as cdk from '@aws-cdk/core'; |
| 53 | +import s3 = require('@aws-cdk/aws-s3'); |
| 54 | +import codecommit = require('@aws-cdk/aws-codecommit'); |
| 55 | +import codepipeline = require('@aws-cdk/aws-codepipeline'); |
| 56 | +import codepipeline_actions = require('@aws-cdk/aws-codepipeline-actions'); |
| 57 | +import codebuild = require('@aws-cdk/aws-codebuild'); |
| 58 | + |
| 59 | +export class PipelineStack extends cdk.Stack { |
| 60 | + constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { |
| 61 | + super(scope, id, props); |
| 62 | + |
| 63 | + // The code that defines your stack goes here |
| 64 | + const artifactsBucket = new s3.Bucket(this, "ArtifactsBucket"); |
| 65 | + |
| 66 | + // Import existing CodeCommit sam-app repository |
| 67 | + const codeRepo = codecommit.Repository.fromRepositoryName( |
| 68 | + this, |
| 69 | + 'AppRepository', // Logical name within CloudFormation |
| 70 | + 'sam-app' // Repository name |
| 71 | + ); |
| 72 | + |
| 73 | + // Pipeline creation starts |
| 74 | + const pipeline = new codepipeline.Pipeline(this, 'Pipeline', { |
| 75 | + artifactBucket: artifactsBucket |
| 76 | + }); |
| 77 | + |
| 78 | + // Declare source code as an artifact |
| 79 | + const sourceOutput = new codepipeline.Artifact(); |
| 80 | + |
| 81 | + // Add source stage to pipeline |
| 82 | + pipeline.addStage({ |
| 83 | + stageName: 'Source', |
| 84 | + actions: [ |
| 85 | + new codepipeline_actions.CodeCommitSourceAction({ |
| 86 | + actionName: 'CodeCommit_Source', |
| 87 | + repository: codeRepo, |
| 88 | + output: sourceOutput, |
| 89 | + }), |
| 90 | + ], |
| 91 | + }); |
| 92 | + |
| 93 | + // Declare build output as artifacts |
| 94 | + const buildOutput = new codepipeline.Artifact(); |
| 95 | + |
| 96 | + // Declare a new CodeBuild project |
| 97 | + const buildProject = new codebuild.PipelineProject(this, 'Build', { |
| 98 | + environment: { buildImage: codebuild.LinuxBuildImage.AMAZON_LINUX_2_2 }, |
| 99 | + environmentVariables: { |
| 100 | + 'PACKAGE_BUCKET': { |
| 101 | + value: artifactsBucket.bucketName |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + // Add the build stage to our pipeline |
| 107 | + pipeline.addStage({ |
| 108 | + stageName: 'Build', |
| 109 | + actions: [ |
| 110 | + new codepipeline_actions.CodeBuildAction({ |
| 111 | + actionName: 'Build', |
| 112 | + project: buildProject, |
| 113 | + input: sourceOutput, |
| 114 | + outputs: [buildOutput], |
| 115 | + }), |
| 116 | + ], |
| 117 | + }); |
| 118 | + |
| 119 | + } |
| 120 | +} |
| 121 | +{{< / highlight >}} |
| 122 | +{{% /expand%}} |
| 123 | + |
| 124 | +### Deploy the pipeline |
| 125 | + |
| 126 | +From your terminal, run the following commands to deploy the pipeline: |
| 127 | + |
| 128 | +``` |
| 129 | +npm run build |
| 130 | +cdk deploy |
| 131 | +``` |
| 132 | + |
| 133 | +### Verify pipeline creation |
| 134 | + |
| 135 | +Navigate to the [AWS CodePipeline Console](https://console.aws.amazon.com/codesuite/codepipeline/home) and click on your newly created pipeline! |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | +The Build step should have failed. **Don't worry! this is expected** because we haven't specified what commands to run during the build yet, so AWS CodeBuild doesn't know how to build our Serverless application. |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +Let's fix that. |
0 commit comments