|
| 1 | +# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 4 | +# software and associated documentation files (the "Software"), to deal in the Software |
| 5 | +# without restriction, including without limitation the rights to use, copy, modify, |
| 6 | +# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 7 | +# permit persons to whom the Software is furnished to do so. |
| 8 | +# |
| 9 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 10 | +# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 11 | +# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 12 | +# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 13 | +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 14 | +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 15 | +AWSTemplateFormatVersion: 2010-09-09 |
| 16 | +Description: > |
| 17 | + Deployment pipeline for Distributed Load Test Using AWS Fargate. |
| 18 | + It creates a CodeBuild project used to configure and run a load test. |
| 19 | +
|
| 20 | +Parameters: |
| 21 | + GitRepositoryName: |
| 22 | + Type: String |
| 23 | + DockerRegistryURL: |
| 24 | + Type: String |
| 25 | + DockerRegistryArn: |
| 26 | + Type: String |
| 27 | + ContainersPerRegion: |
| 28 | + Type: Number |
| 29 | + Default: 3 |
| 30 | + |
| 31 | +Resources: |
| 32 | + CodeRepository: |
| 33 | + Type: AWS::CodeCommit::Repository |
| 34 | + Properties: |
| 35 | + RepositoryDescription: "Distributed load testing using AWS Fargate" |
| 36 | + RepositoryName: !Ref GitRepositoryName |
| 37 | + |
| 38 | + ArtifactStoreBucket: |
| 39 | + Type: AWS::S3::Bucket |
| 40 | + Properties: |
| 41 | + VersioningConfiguration: |
| 42 | + Status: Enabled |
| 43 | + |
| 44 | + CodeBuild: |
| 45 | + Type: AWS::CodeBuild::Project |
| 46 | + Properties: |
| 47 | + Description: Builds distributed load testing suite |
| 48 | + TimeoutInMinutes: 20 |
| 49 | + ServiceRole: !GetAtt CodeBuildRole.Arn |
| 50 | + Artifacts: |
| 51 | + Type: CODEPIPELINE |
| 52 | + Environment: |
| 53 | + Type: LINUX_CONTAINER |
| 54 | + ComputeType: BUILD_GENERAL1_MEDIUM |
| 55 | + Image: "aws/codebuild/docker:18.09.0" |
| 56 | + EnvironmentVariables: |
| 57 | + - |
| 58 | + Name: DOCKER_REGISTRY_URL |
| 59 | + Value: !Ref DockerRegistryURL |
| 60 | + Source: |
| 61 | + Type: CODEPIPELINE |
| 62 | + BuildSpec: !Sub | |
| 63 | + version: 0.2 |
| 64 | + phases: |
| 65 | + pre_build: |
| 66 | + commands: |
| 67 | + - echo $DOCKER_REGISTRY_URL |
| 68 | + - echo $CODEBUILD_RESOLVED_SOURCE_VERSION |
| 69 | + - aws --version |
| 70 | + - $(aws ecr get-login --region ${AWS::Region} --no-include-email) |
| 71 | + build: |
| 72 | + commands: |
| 73 | + - docker build -t $DOCKER_REGISTRY_URL:latest . |
| 74 | + - docker tag $DOCKER_REGISTRY_URL:latest $DOCKER_REGISTRY_URL:$CODEBUILD_RESOLVED_SOURCE_VERSION |
| 75 | + post_build: |
| 76 | + commands: |
| 77 | + - docker push $DOCKER_REGISTRY_URL:latest |
| 78 | + - docker push $DOCKER_REGISTRY_URL:$CODEBUILD_RESOLVED_SOURCE_VERSION |
| 79 | +
|
| 80 | + # Leverage CodeBuild to kick start the test suite. |
| 81 | + # It could very well be replaced with a Lambda function |
| 82 | + LoadTestRunner: |
| 83 | + Type: AWS::CodeBuild::Project |
| 84 | + Properties: |
| 85 | + Description: Runs the load tests in Fargate |
| 86 | + TimeoutInMinutes: 240 # 2 hours |
| 87 | + ServiceRole: !GetAtt CodeBuildRole.Arn |
| 88 | + Artifacts: |
| 89 | + Type: CODEPIPELINE |
| 90 | + Environment: |
| 91 | + Type: LINUX_CONTAINER |
| 92 | + ComputeType: BUILD_GENERAL1_SMALL |
| 93 | + Image: "aws/codebuild/python:3.6.5" |
| 94 | + Source: |
| 95 | + Type: CODEPIPELINE |
| 96 | + BuildSpec: !Sub | |
| 97 | + version: 0.2 |
| 98 | + phases: |
| 99 | + build: |
| 100 | + commands: |
| 101 | + - cd bin |
| 102 | + - python runner.py |
| 103 | +
|
| 104 | + CodePipeline: |
| 105 | + Type: AWS::CodePipeline::Pipeline |
| 106 | + Properties: |
| 107 | + RoleArn: !GetAtt CodePipelineRole.Arn |
| 108 | + ArtifactStore: |
| 109 | + Type: S3 |
| 110 | + Location: !Ref ArtifactStoreBucket |
| 111 | + Stages: |
| 112 | + - |
| 113 | + Name: Source |
| 114 | + Actions: |
| 115 | + - |
| 116 | + Name: Source |
| 117 | + ActionTypeId: |
| 118 | + Category: Source |
| 119 | + Provider: CodeCommit |
| 120 | + Owner: AWS |
| 121 | + Version: 1 |
| 122 | + OutputArtifacts: |
| 123 | + - Name: SourceOutput |
| 124 | + Configuration: |
| 125 | + RepositoryName: !GetAtt CodeRepository.Name |
| 126 | + BranchName: 'master' |
| 127 | + |
| 128 | + - |
| 129 | + Name: Build |
| 130 | + Actions: |
| 131 | + - |
| 132 | + Name: Build |
| 133 | + ActionTypeId: |
| 134 | + Category: Build |
| 135 | + Owner: AWS |
| 136 | + Version: 1 |
| 137 | + Provider: CodeBuild |
| 138 | + InputArtifacts: |
| 139 | + - Name: SourceOutput |
| 140 | + OutputArtifacts: |
| 141 | + - Name: BuildOutput |
| 142 | + Configuration: |
| 143 | + ProjectName: |
| 144 | + Ref: CodeBuild |
| 145 | + |
| 146 | + - |
| 147 | + Name: LoadTest |
| 148 | + Actions: |
| 149 | + - Name: Runner |
| 150 | + ActionTypeId: |
| 151 | + Category: Build |
| 152 | + Owner: AWS |
| 153 | + Version: 1 |
| 154 | + Provider: CodeBuild |
| 155 | + InputArtifacts: |
| 156 | + - Name: SourceOutput |
| 157 | + Configuration: |
| 158 | + ProjectName: |
| 159 | + Ref: LoadTestRunner |
| 160 | + |
| 161 | + # Shared IAM policy for CodePipeline and CodeBuild projects for simplicity. |
| 162 | + # We could be more strict and create a separate role for each build |
| 163 | + # project and another one for the pipeline. |
| 164 | + BuildAndPipelinePolicy: |
| 165 | + Type: AWS::IAM::Policy |
| 166 | + Properties: |
| 167 | + PolicyName: "distributed-load-testing-using-aws-fargate" |
| 168 | + Roles: |
| 169 | + - !Ref CodeBuildRole |
| 170 | + - !Ref CodePipelineRole |
| 171 | + PolicyDocument: |
| 172 | + Version: 2012-10-17 |
| 173 | + Statement: |
| 174 | + - Effect: Allow |
| 175 | + Resource: |
| 176 | + - !GetAtt ArtifactStoreBucket.Arn |
| 177 | + - !Join |
| 178 | + - '' |
| 179 | + - - !GetAtt ArtifactStoreBucket.Arn |
| 180 | + - '/*' |
| 181 | + Action: |
| 182 | + - "s3:*" |
| 183 | + - Effect: Allow |
| 184 | + Resource: !GetAtt CodeRepository.Arn |
| 185 | + Action: |
| 186 | + - "codecommit:GetBranch" |
| 187 | + - "codecommit:GetCommit" |
| 188 | + - "codecommit:UploadArchive" |
| 189 | + - "codecommit:GetUploadArchiveStatus" |
| 190 | + - "codecommit:CancelUploadArchive" |
| 191 | + - Effect: Allow |
| 192 | + Resource: !Ref DockerRegistryArn |
| 193 | + Action: |
| 194 | + - "ecr:DescribeImages" |
| 195 | + - "ecr:PutImage" |
| 196 | + - "ecr:UploadLayerPart" |
| 197 | + - "ecr:CompleteLayerUpload" |
| 198 | + - "ecr:InitiateLayerUpload" |
| 199 | + - "ecr:GetDownloadUrlForLayer" |
| 200 | + - "ecr:ListImages" |
| 201 | + - "ecr:BatchCheckLayerAvailability" |
| 202 | + - "ecr:GetRepositoryPolicy" |
| 203 | + - Effect: Allow |
| 204 | + Resource: "*" |
| 205 | + Action: |
| 206 | + - "codebuild:StartBuild" |
| 207 | + - "codebuild:StopBuild" |
| 208 | + - "codebuild:BatchGetBuilds" |
| 209 | + - "logs:CreateLogStream" |
| 210 | + - "logs:CreateLogGroup" |
| 211 | + - "logs:PutLogEvents" |
| 212 | + - "ecr:GetAuthorizationToken" |
| 213 | + - "cloudformation:DescribeStacks" |
| 214 | + - "ecs:RunTask" |
| 215 | + - "iam:PassRole" |
| 216 | + |
| 217 | + CodeBuildRole: |
| 218 | + Type: AWS::IAM::Role |
| 219 | + Properties: |
| 220 | + AssumeRolePolicyDocument: |
| 221 | + Version: 2012-10-17 |
| 222 | + Statement: |
| 223 | + - |
| 224 | + Effect: Allow |
| 225 | + Principal: |
| 226 | + Service: |
| 227 | + - codebuild.amazonaws.com |
| 228 | + Action: |
| 229 | + - sts:AssumeRole |
| 230 | + Path: "/" |
| 231 | + |
| 232 | + CodePipelineRole: |
| 233 | + Type: AWS::IAM::Role |
| 234 | + Properties: |
| 235 | + AssumeRolePolicyDocument: |
| 236 | + Version: 2012-10-17 |
| 237 | + Statement: |
| 238 | + - Effect: Allow |
| 239 | + Principal: |
| 240 | + Service: |
| 241 | + - codepipeline.amazonaws.com |
| 242 | + Action: |
| 243 | + - sts:AssumeRole |
| 244 | + Path: "/" |
| 245 | + |
| 246 | +Outputs: |
| 247 | + GitRepoHttpUrl: |
| 248 | + Value: !GetAtt CodeRepository.CloneUrlHttp |
| 249 | + GitRepoSshUrl: |
| 250 | + Value: !GetAtt CodeRepository.CloneUrlSsh |
0 commit comments