├── .gitignore ├── images ├── CrossAccBlog.png ├── architecture.png ├── CrossAccBlog-WithText.png └── details-cross-account-pipeline.png ├── NOTICE ├── DevAccount └── toolsacct-codepipeline-codecommit.yaml ├── single-click-cross-account-pipeline.sh ├── ToolsAcct ├── pre-reqs.yaml └── code-pipeline.yaml ├── TestAccount └── toolsacct-codepipeline-cloudformation-deployer.yaml ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /images/CrossAccBlog.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/aws-refarch-cross-account-pipeline/HEAD/images/CrossAccBlog.png -------------------------------------------------------------------------------- /images/architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/aws-refarch-cross-account-pipeline/HEAD/images/architecture.png -------------------------------------------------------------------------------- /images/CrossAccBlog-WithText.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/aws-refarch-cross-account-pipeline/HEAD/images/CrossAccBlog-WithText.png -------------------------------------------------------------------------------- /images/details-cross-account-pipeline.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/aws-refarch-cross-account-pipeline/HEAD/images/details-cross-account-pipeline.png -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at 4 | 5 | http://aws.amazon.com/apache2.0/ 6 | 7 | or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 8 | 9 | refarch-cross-account-codepipeline 10 | Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 11 | -------------------------------------------------------------------------------- /DevAccount/toolsacct-codepipeline-codecommit.yaml: -------------------------------------------------------------------------------- 1 | # Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with 3 | # the License. A copy of the License is located at 4 | # http://aws.amazon.com/apache2.0/ 5 | # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 6 | # CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and 7 | # limitations under the License. 8 | 9 | AWSTemplateFormatVersion: '2010-09-09' 10 | Description: Cross Account Role to Allow Access to CodePipeline in Tools Account 11 | Parameters: 12 | ToolsAccount: 13 | Description: AWS AccountNumber for tools account 14 | Type: Number 15 | CMKARN: 16 | Description: ARN of the KMS CMK creates in Tools account 17 | Type: String 18 | Resources: 19 | Role: 20 | Type: AWS::IAM::Role 21 | Properties: 22 | RoleName: !Sub ToolsAcctCodePipelineCodeCommitRole 23 | AssumeRolePolicyDocument: 24 | Version: 2012-10-17 25 | Statement: 26 | - 27 | Effect: Allow 28 | Principal: 29 | AWS: 30 | - !Ref ToolsAccount 31 | Action: 32 | - sts:AssumeRole 33 | Path: / 34 | 35 | Policy: 36 | Type: AWS::IAM::Policy 37 | Properties: 38 | PolicyName: !Sub ToolsAcctCodePipelineCodeCommitPolicy 39 | PolicyDocument: 40 | Version: 2012-10-17 41 | Statement: 42 | - 43 | Effect: Allow 44 | Action: 45 | - codecommit:BatchGetRepositories 46 | - codecommit:Get* 47 | - codecommit:GitPull 48 | - codecommit:List* 49 | - codecommit:CancelUploadArchive 50 | - codecommit:UploadArchive 51 | - s3:* 52 | Resource: "*" 53 | - 54 | Effect: Allow 55 | Action: 56 | - kms:* 57 | Resource: !Ref CMKARN 58 | Roles: 59 | - 60 | !Ref Role -------------------------------------------------------------------------------- /single-click-cross-account-pipeline.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | echo -n "Enter ToolsAccount > " 4 | read ToolsAccount 5 | echo -n "Enter ToolsAccount ProfileName for AWS Cli operations> " 6 | read ToolsAccountProfile 7 | echo -n "Enter Dev Account > " 8 | read DevAccount 9 | echo -n "Enter DevAccount ProfileName for AWS Cli operations> " 10 | read DevAccountProfile 11 | echo -n "Enter Test Account > " 12 | read TestAccount 13 | echo -n "Enter TestAccount ProfileName for AWS Cli operations> " 14 | read TestAccountProfile 15 | echo -n "Enter Prod Account > " 16 | read ProdAccount 17 | echo -n "Enter ProdAccount ProfileName for AWS Cli operations> " 18 | read ProdAccountProfile 19 | echo -n "Deploying pre-requisite stack to the tools account... " 20 | aws cloudformation deploy --stack-name pre-reqs --template-file ToolsAcct/pre-reqs.yaml --parameter-overrides DevAccount=$DevAccount TestAccount=$TestAccount ProductionAccount=$ProdAccount --profile $ToolsAccountProfile 21 | echo -n "Fetching S3 bucket and CMK ARN from CloudFormation automatically..." 22 | get_s3_command="aws cloudformation describe-stacks --stack-name pre-reqs --profile $ToolsAccountProfile --query \"Stacks[0].Outputs[?OutputKey=='ArtifactBucket'].OutputValue\" --output text" 23 | S3Bucket=$(eval $get_s3_command) 24 | echo -n "Got S3 bucket name: $S3Bucket" 25 | 26 | get_cmk_command="aws cloudformation describe-stacks --stack-name pre-reqs --profile $ToolsAccountProfile --query \"Stacks[0].Outputs[?OutputKey=='CMK'].OutputValue\" --output text" 27 | CMKArn=$(eval $get_cmk_command) 28 | echo -n "Got CMK ARN: $CMKArn" 29 | 30 | echo -n "Executing in DEV Account" 31 | aws cloudformation deploy --stack-name toolsacct-codepipeline-role --template-file DevAccount/toolsacct-codepipeline-codecommit.yaml --capabilities CAPABILITY_NAMED_IAM --parameter-overrides ToolsAccount=$ToolsAccount CMKARN=$CMKArn --profile $DevAccountProfile 32 | 33 | echo -n "Executing in TEST Account" 34 | aws cloudformation deploy --stack-name toolsacct-codepipeline-cloudformation-role --template-file TestAccount/toolsacct-codepipeline-cloudformation-deployer.yaml --capabilities CAPABILITY_NAMED_IAM --parameter-overrides ToolsAccount=$ToolsAccount CMKARN=$CMKArn S3Bucket=$S3Bucket --profile $TestAccountProfile 35 | 36 | echo -n "Executing in PROD Account" 37 | aws cloudformation deploy --stack-name toolsacct-codepipeline-cloudformation-role --template-file TestAccount/toolsacct-codepipeline-cloudformation-deployer.yaml --capabilities CAPABILITY_NAMED_IAM --parameter-overrides ToolsAccount=$ToolsAccount CMKARN=$CMKArn S3Bucket=$S3Bucket --profile $ProdAccountProfile 38 | 39 | echo -n "Creating Pipeline in Tools Account" 40 | aws cloudformation deploy --stack-name sample-lambda-pipeline --template-file ToolsAcct/code-pipeline.yaml --parameter-overrides DevAccount=$DevAccount TestAccount=$TestAccount ProductionAccount=$ProdAccount CMKARN=$CMKArn S3Bucket=$S3Bucket --capabilities CAPABILITY_NAMED_IAM --profile $ToolsAccountProfile 41 | 42 | echo -n "Adding Permissions to the CMK" 43 | aws cloudformation deploy --stack-name pre-reqs --template-file ToolsAcct/pre-reqs.yaml --parameter-overrides CodeBuildCondition=true --profile $ToolsAccountProfile 44 | 45 | echo -n "Adding Permissions to the Cross Accounts" 46 | aws cloudformation deploy --stack-name sample-lambda-pipeline --template-file ToolsAcct/code-pipeline.yaml --parameter-overrides CrossAccountCondition=true --capabilities CAPABILITY_NAMED_IAM --profile $ToolsAccountProfile -------------------------------------------------------------------------------- /ToolsAcct/pre-reqs.yaml: -------------------------------------------------------------------------------- 1 | # Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with 3 | # the License. A copy of the License is located at 4 | # http://aws.amazon.com/apache2.0/ 5 | # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 6 | # CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and 7 | # limitations under the License. 8 | 9 | AWSTemplateFormatVersion: '2010-09-09' 10 | Description: Creates a CMK in KMS and grants access to other accounts 11 | Parameters: 12 | DevAccount: 13 | Description: AWS AccountNumber for dev 14 | Type: Number 15 | TestAccount: 16 | Description: AWS AccountNumber for test 17 | Type: Number 18 | ProductionAccount: 19 | Description: AWS AccountNumber for production 20 | Type: Number 21 | ProjectName: 22 | Description: Name of the Project 23 | Type: String 24 | Default: sample-lambda 25 | CodeBuildCondition: 26 | Description: Conditionally adds the access required by code build project role 27 | Type: String 28 | Default: false 29 | Conditions: 30 | AddCodeBuildResource: !Equals [ !Ref CodeBuildCondition, true ] 31 | Resources: 32 | KMSKey: 33 | Type: AWS::KMS::Key 34 | Properties: 35 | Description: Used by Assumed Roles in Dev/Test/Prod accounts to Encrypt/Decrypt code 36 | EnableKeyRotation: true 37 | KeyPolicy: 38 | Version: "2012-10-17" 39 | Id: !Ref AWS::StackName 40 | Statement: 41 | - 42 | Sid: Allows admin of the key 43 | Effect: Allow 44 | Principal: 45 | AWS: !Sub arn:aws:iam::${AWS::AccountId}:root 46 | Action: 47 | - "kms:Create*" 48 | - "kms:Describe*" 49 | - "kms:Enable*" 50 | - "kms:List*" 51 | - "kms:Put*" 52 | - "kms:Update*" 53 | - "kms:Revoke*" 54 | - "kms:Disable*" 55 | - "kms:Get*" 56 | - "kms:Delete*" 57 | - "kms:ScheduleKeyDeletion" 58 | - "kms:CancelKeyDeletion" 59 | Resource: "*" 60 | - 61 | Sid: Allow use of the key for CryptoGraphy Lambda 62 | Effect: Allow 63 | Principal: 64 | AWS: 65 | - !Sub arn:aws:iam::${ProductionAccount}:root 66 | - !Sub arn:aws:iam::${TestAccount}:root 67 | - !Sub arn:aws:iam::${DevAccount}:root 68 | - !If 69 | - AddCodeBuildResource 70 | - !Sub arn:aws:iam::${AWS::AccountId}:role/${ProjectName}-CodeBuildRole 71 | - !Ref AWS::NoValue 72 | Action: 73 | - kms:Encrypt 74 | - kms:Decrypt 75 | - kms:ReEncrypt* 76 | - kms:GenerateDataKey* 77 | - kms:DescribeKey 78 | Resource: "*" 79 | KMSAlias: 80 | Type: AWS::KMS::Alias 81 | Properties: 82 | AliasName: !Sub alias/codepipeline-crossaccounts 83 | TargetKeyId: !Ref KMSKey 84 | ArtifactBucket: 85 | Type: AWS::S3::Bucket 86 | DeletionPolicy: Retain 87 | 88 | Outputs: 89 | CMK: 90 | Value: !GetAtt [KMSKey,Arn] 91 | ArtifactBucket: 92 | Value: !Ref ArtifactBucket 93 | 94 | -------------------------------------------------------------------------------- /TestAccount/toolsacct-codepipeline-cloudformation-deployer.yaml: -------------------------------------------------------------------------------- 1 | # Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with 3 | # the License. A copy of the License is located at 4 | # http://aws.amazon.com/apache2.0/ 5 | # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 6 | # CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and 7 | # limitations under the License. 8 | 9 | AWSTemplateFormatVersion: '2010-09-09' 10 | Description: Role to be assumed by CodePipeline service cross account 11 | Parameters: 12 | S3Bucket: 13 | Description: S3 Bucket in Tools Account, which holds the artifacts built by codebuild 14 | Type: String 15 | ToolsAccount: 16 | Description: AWS AccountNumber for Tools 17 | Type: Number 18 | CMKARN: 19 | Description: ARN of the KMS CMK creates in Tools account 20 | Type: String 21 | Resources: 22 | CFRole: 23 | Type: AWS::IAM::Role 24 | Properties: 25 | RoleName: !Sub ToolsAcctCodePipelineCloudFormationRole 26 | AssumeRolePolicyDocument: 27 | Version: 2012-10-17 28 | Statement: 29 | - 30 | Effect: Allow 31 | Principal: 32 | AWS: 33 | - !Ref ToolsAccount 34 | Action: 35 | - sts:AssumeRole 36 | Path: / 37 | CFPolicy: 38 | Type: AWS::IAM::Policy 39 | Properties: 40 | PolicyName: !Sub ToolsAcctCodePipelineCloudFormationPolicy 41 | PolicyDocument: 42 | Version: 2012-10-17 43 | Statement: 44 | - 45 | Effect: Allow 46 | Action: 47 | - cloudformation:* 48 | - s3:* 49 | - iam:PassRole 50 | Resource: "*" 51 | - 52 | Effect: Allow 53 | Action: 54 | - kms:* 55 | Resource: !Ref CMKARN 56 | Roles: 57 | - 58 | !Ref CFRole 59 | CFDeployerRole: 60 | Type: AWS::IAM::Role 61 | Properties: 62 | RoleName: !Sub cloudformationdeployer-role 63 | AssumeRolePolicyDocument: 64 | Version: 2012-10-17 65 | Statement: 66 | - 67 | Effect: Allow 68 | Principal: 69 | Service: 70 | - cloudformation.amazonaws.com 71 | Action: 72 | - sts:AssumeRole 73 | Path: / 74 | CFDeployerPolicy: 75 | Type: AWS::IAM::Policy 76 | Properties: 77 | PolicyName: !Sub cloudformationdeployer-policy 78 | PolicyDocument: 79 | Version: 2012-10-17 80 | Statement: 81 | - 82 | Effect: Allow 83 | Action: 84 | - lambda:AddPermission 85 | - lambda:CreateFunction 86 | - lambda:DeleteFunction 87 | - lambda:InvokeFunction 88 | - lambda:RemovePermission 89 | - lambda:UpdateFunctionCode 90 | - lambda:GetFunctionConfiguration 91 | - lambda:GetFunction 92 | - lambda:UpdateFunctionConfiguration 93 | - lambda:ListTags 94 | - lambda:TagResource 95 | - lambda:UnTagResource 96 | # - events:* # Required for the sample lambda function to work 97 | - iam:CreateRole 98 | - iam:CreatePolicy 99 | - iam:GetRole 100 | - iam:DeleteRole 101 | - iam:PutRolePolicy 102 | - iam:PassRole 103 | - iam:DeleteRolePolicy 104 | - iam:AttachRolePolicy 105 | - cloudformation:* 106 | - iam:DetachRolePolicy 107 | - apigateway:POST # Create API. 108 | - apigateway:GET # Get API info. 109 | - apigateway:PATCH # Update API resources. 110 | - apigateway:DELETE # Delete API incase of rollback. 111 | Resource: "*" 112 | - 113 | Effect: Allow 114 | Action: 115 | - s3:PutObject 116 | - s3:GetBucketPolicy 117 | - s3:GetObject 118 | - s3:ListBucket 119 | Resource: 120 | - !Join ['',['arn:aws:s3:::',!Ref S3Bucket, '/*']] 121 | - !Join ['',['arn:aws:s3:::',!Ref S3Bucket]] 122 | Roles: 123 | - 124 | !Ref CFDeployerRole -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Reference Architecture: Cross Account AWS CodePipeline 2 | 3 | This reference architecture demonstrates how to push code hosted in [AWS CodeCommit](https://aws.amazon.com/codecommit/) repository in Development Account, 4 | use [AWS CodeBuild](https://aws.amazon.com/codebuild/) to do application build, store the output artifacts in S3Bucket and deploy these artifacts to a Test AWS account, validate your deployment then approve the changes to be deployed to the Production Account using [AWS CloudFormation](https://aws.amazon.com/cloudformation/). This orchestration of code movement from code checkin to deployment is securely handled by [AWS CodePipeline](https://aws.amazon.com/codepipeline/). 5 | 6 | ![](images/CrossAccBlog-WithText.png) 7 | 8 | ## Running the example 9 | > You need to create the CodeCommit repository (steps below) before making the pipeline infrastructure. 10 | > When creating the pipeline infrastructure, you can use the `single-click-cross-account-pipeline.sh` script or else follow the "Walkthrough" section of the [blog post](https://aws.amazon.com/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/). 11 | #### Pre-requisites 12 | 1. Install the [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html). 13 | 2. Intall the [SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html). 14 | 3. Clone this repository. 15 | 4. Have the following AWS accounts (if using Control Tower, [this is useful](https://docs.aws.amazon.com/controltower/latest/userguide/account-factory.html#quick-account-provisioning)): 16 | * Tooling 17 | * Development 18 | * Test 19 | * Production 20 | 21 | #### 1. Create a sample application using Serverless Application Model (SAM). 22 | 23 | We will create a sample serverless application locally, then commit the code to CodeCommit. From there, CodePipeline will build the serverless application, deploy it using CloudFormation to a test account then you will accept/reject the change from the pipeline. If you accept the review in CodePipeline, the application will be deployed to production using CloudFormation. 24 | 25 | ##### Create the sample application locally 26 | 27 | From your terminal application/command line, execute the following command: 28 | 29 | ```console 30 | sam init 31 | # Use the values: 32 | # - Template source: Quick Start template 33 | # - Runtime: Python 3.7 34 | # - Project Name: sample-lambda 35 | ``` 36 | 37 | This creates a directory named `sample-lambda` in your current directory, which contains the code for a serverless application. 38 | 39 | Navigate to the project folder and initialize the git client 40 | ```console 41 | cd sample-lambda/ 42 | git init 43 | ``` 44 | 45 | #### 2. Create [AWS CodeCommit](code-commit-url) repository in Development Account 46 | ##### Console Method 47 | Follow the [instructions here](http://docs.aws.amazon.com/codecommit/latest/userguide/getting-started.html#getting-started-create-repo) to create a CodeCommit repository in the Development Account. Name your repository as sample-lambda. 48 | 49 | ##### Terminal Method 50 | From your terminal application, execute the following command. You may refer [here](http://docs.aws.amazon.com/codecommit/latest/userguide/how-to-create-repository.html#how-to-create-repository-cli) for further details on installing the AWS CLI if needed. 51 | 52 | ```console 53 | aws codecommit create-repository --repository-name sample-lambda --repository-description "Sample Serverless App" --profile {{DEV-ACCOUNT-PROFILE}} 54 | ``` 55 | 56 | Note the cloneUrlHttp URL in the response from above CLI. 57 | 58 | #### 3. Add a new remote 59 | 60 | From your terminal application, execute the following command: 61 | 62 | ```console 63 | git remote add AWSCodeCommit {{HTTP_CLONE_URL_FROM_STEP_2}} 64 | ``` 65 | 66 | Follow the instructions [here](http://docs.aws.amazon.com/codecommit/latest/userguide/setting-up.html) for local git setup required to push code to CodeCommit repository. 67 | 68 | > Tip: The AWS CodeCommit Helper is useful to obtain Codecommit credentials using a profile, for example: 69 | >```console 70 | >git config --global credential.helper '!aws --profile {{YOUR-PROFILE-HERE}} codecommit credential-helper $@' 71 | >``` 72 | 73 | #### 4. Push the code AWS CodeCommit 74 | 75 | From your terminal application, execute the following command: 76 | 77 | ```console 78 | git add . 79 | git commit -m "First push of my SAM app!" 80 | git push AWSCodeCommit master 81 | ``` 82 | 83 | #### 5. See the pipeline in action. 84 | Once you have your pipeline configured [as per the blog post](https://aws.amazon.com/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/) across your tools, development, test and production AWS accounts, codepipeline will listen for new deployments to your 'sample-lambda' repository. You can configure the pipeline by following the walkthrough in the blog post or by running the `single-click-cross-account-pipeline.sh` script in this repo. Once it's spun up, push a change to the CodeCommit repo you just made then log in to your tools AWS account to ensure your codepipeline execution has kicked off. 85 | 86 | #### Next Steps 87 | * If you want to deploy a different type of application, you will need to edit the buildspec file defined in the [`code-pipeline.yaml`](https://github.com/awslabs/aws-refarch-cross-account-pipeline/blob/master/ToolsAcct/code-pipeline.yaml) file. 88 | * You will also need to change the permissions of the roles deployed to the test/dev accounts depending on what type of resources you are deploying. This is in the [`toolsacct-codepipeline-cloudformation-deployer.yaml`](https://github.com/awslabs/aws-refarch-cross-account-pipeline/blob/master/TestAccount/toolsacct-codepipeline-cloudformation-deployer.yaml#L74) file which gets deployed to the Test & Prod accounts in step 3 of the [blog instructions](https://aws.amazon.com/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/). 89 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2016 Amazon Web Services 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /ToolsAcct/code-pipeline.yaml: -------------------------------------------------------------------------------- 1 | # Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with 3 | # the License. A copy of the License is located at 4 | # http://aws.amazon.com/apache2.0/ 5 | # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 6 | # CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and 7 | # limitations under the License. 8 | 9 | AWSTemplateFormatVersion: '2010-09-09' 10 | Description: CodePipeline for the Sample Lambda Function 11 | Parameters: 12 | ProjectName: 13 | Description: Name of the Project 14 | Type: String 15 | Default: sample-lambda 16 | S3Bucket: 17 | Description: S3 Bucket, which will hold the artifacts 18 | Type: String 19 | DevAccount: 20 | Description: AWS AccountNumber for dev 21 | Type: Number 22 | TestAccount: 23 | Description: AWS AccountNumber for test 24 | Type: Number 25 | ProductionAccount: 26 | Description: AWS AccountNumber for production 27 | Type: Number 28 | CMKARN: 29 | Description: ARN of the KMS CMK creates in Tools account 30 | Type: String 31 | CrossAccountCondition: 32 | Description: Conditionally creates the resources for cross account access 33 | Type: String 34 | Default: false 35 | 36 | Conditions: 37 | AddCodeBuildResource: !Equals [ !Ref CrossAccountCondition, true ] 38 | 39 | Resources: 40 | 41 | BuildProjectRole: 42 | Type: AWS::IAM::Role 43 | Properties: 44 | RoleName: !Sub ${ProjectName}-CodeBuildRole 45 | AssumeRolePolicyDocument: 46 | Version: 2012-10-17 47 | Statement: 48 | - 49 | Effect: Allow 50 | Principal: 51 | Service: 52 | - codebuild.amazonaws.com 53 | Action: 54 | - sts:AssumeRole 55 | Path: / 56 | BuildProjectPolicy: 57 | Type: AWS::IAM::Policy 58 | DependsOn: S3BucketPolicy 59 | Properties: 60 | PolicyName: !Sub ${ProjectName}-CodeBuildPolicy 61 | PolicyDocument: 62 | Version: 2012-10-17 63 | Statement: 64 | - 65 | Effect: Allow 66 | Action: 67 | - s3:PutObject 68 | - s3:GetBucketPolicy 69 | - s3:GetObject 70 | - s3:ListBucket 71 | 72 | Resource: 73 | - !Join ['',['arn:aws:s3:::',!Ref S3Bucket, '/*']] 74 | - !Join ['',['arn:aws:s3:::',!Ref S3Bucket]] 75 | - 76 | Effect: Allow 77 | Action: 78 | - kms:* 79 | Resource: !Ref CMKARN 80 | - 81 | Effect: Allow 82 | Action: 83 | - logs:CreateLogGroup 84 | - logs:CreateLogStream 85 | - logs:PutLogEvents 86 | Resource: arn:aws:logs:*:*:* 87 | Roles: 88 | - 89 | !Ref BuildProjectRole 90 | BuildProject: 91 | Type: AWS::CodeBuild::Project 92 | Properties: 93 | Name: !Ref ProjectName 94 | Description: !Ref ProjectName 95 | EncryptionKey: !Ref CMKARN 96 | ServiceRole: !GetAtt BuildProjectRole.Arn 97 | Artifacts: 98 | Type: CODEPIPELINE 99 | Environment: 100 | Type: linuxContainer 101 | ComputeType: BUILD_GENERAL1_SMALL 102 | Image: aws/codebuild/amazonlinux2-x86_64-standard:3.0 103 | EnvironmentVariables: 104 | - Name: S3Bucket 105 | Value: !Ref S3Bucket 106 | - Name: KMSKey 107 | Value: !Ref CMKARN 108 | Source: 109 | Type: CODEPIPELINE 110 | BuildSpec: | 111 | version: 0.2 112 | phases: 113 | install: 114 | runtime-versions: 115 | python: 3.7 116 | build: 117 | commands: 118 | - pip install --upgrade pip 119 | - pip install pipenv --user 120 | - pip install awscli aws-sam-cli 121 | - sam package --template-file template.yaml --s3-bucket $S3Bucket --output-template-file packaged-template.yml --region $AWS_REGION 122 | artifacts: 123 | files: 124 | - packaged-template.yml 125 | 126 | TimeoutInMinutes: 10 127 | Tags: 128 | - Key: Name 129 | Value: !Ref ProjectName 130 | PipeLineRole: 131 | Type: AWS::IAM::Role 132 | Properties: 133 | RoleName: !Sub ${ProjectName}-codepipeline-role 134 | AssumeRolePolicyDocument: 135 | Version: 2012-10-17 136 | Statement: 137 | - 138 | Effect: Allow 139 | Principal: 140 | Service: 141 | - codepipeline.amazonaws.com 142 | Action: 143 | - sts:AssumeRole 144 | Path: / 145 | PipelinePolicy: 146 | Type: AWS::IAM::Policy 147 | DependsOn: S3BucketPolicy 148 | Properties: 149 | PolicyName: !Sub ${ProjectName}-codepipeline-policy 150 | PolicyDocument: 151 | Version: 2012-10-17 152 | Statement: 153 | - 154 | Effect: Allow 155 | Action: 156 | - codepipeline:* 157 | - iam:ListRoles 158 | - cloudformation:Describe* 159 | - cloudFormation:List* 160 | - codecommit:List* 161 | - codecommit:Get* 162 | - codecommit:GitPull 163 | - codecommit:UploadArchive 164 | - codecommit:CancelUploadArchive 165 | - codebuild:BatchGetBuilds 166 | - codebuild:StartBuild 167 | - cloudformation:CreateStack 168 | - cloudformation:DeleteStack 169 | - cloudformation:DescribeStacks 170 | - cloudformation:UpdateStack 171 | - cloudformation:CreateChangeSet 172 | - cloudformation:DeleteChangeSet 173 | - cloudformation:DescribeChangeSet 174 | - cloudformation:ExecuteChangeSet 175 | - cloudformation:SetStackPolicy 176 | - cloudformation:ValidateTemplate 177 | - iam:PassRole 178 | - s3:ListAllMyBuckets 179 | - s3:GetBucketLocation 180 | Resource: 181 | - "*" 182 | - 183 | Effect: Allow 184 | Action: 185 | - kms:Decrypt 186 | Resource: !Ref CMKARN 187 | - 188 | Effect: Allow 189 | Action: 190 | - s3:PutObject 191 | - s3:GetBucketPolicy 192 | - s3:GetObject 193 | - s3:ListBucket 194 | Resource: 195 | - !Join ['',['arn:aws:s3:::',!Ref S3Bucket, '/*']] 196 | - !Join ['',['arn:aws:s3:::',!Ref S3Bucket]] 197 | - 198 | Effect: Allow 199 | Action: 200 | - sts:AssumeRole 201 | Resource: 202 | - !Sub arn:aws:iam::${DevAccount}:role/ToolsAcctCodePipelineCodeCommitRole 203 | - !Sub arn:aws:iam::${ProductionAccount}:role/ToolsAcctCodePipelineCloudFormationRole 204 | - !Sub arn:aws:iam::${TestAccount}:role/ToolsAcctCodePipelineCloudFormationRole 205 | 206 | Roles: 207 | - 208 | !Ref PipeLineRole 209 | Pipeline: 210 | Type: AWS::CodePipeline::Pipeline 211 | Properties: 212 | RoleArn: !GetAtt PipeLineRole.Arn 213 | Name: !Ref AWS::StackName 214 | Stages: 215 | - Name: Source 216 | Actions: 217 | - Name: App 218 | ActionTypeId: 219 | Category: Source 220 | Owner: AWS 221 | Version: 1 222 | Provider: CodeCommit 223 | Configuration: 224 | RepositoryName: !Ref ProjectName 225 | BranchName: master 226 | OutputArtifacts: 227 | - Name: SCCheckoutArtifact 228 | RunOrder: 1 229 | #RoleArn: !Sub arn:aws:iam::${DevAccount}:role/ToolsAcctCodePipelineCodeCommitRole 230 | RoleArn: 231 | Fn::If: 232 | - AddCodeBuildResource 233 | - !Sub arn:aws:iam::${DevAccount}:role/ToolsAcctCodePipelineCodeCommitRole 234 | - !Ref AWS::NoValue 235 | - 236 | Name: Build 237 | Actions: 238 | - 239 | Name: Build 240 | ActionTypeId: 241 | Category: Build 242 | Owner: AWS 243 | Version: 1 244 | Provider: CodeBuild 245 | Configuration: 246 | ProjectName: !Ref BuildProject 247 | RunOrder: 1 248 | InputArtifacts: 249 | - Name: SCCheckoutArtifact 250 | OutputArtifacts: 251 | - Name: BuildOutput 252 | - Name: DeployToTest 253 | Actions: 254 | - Name: CreateChangeSetTest 255 | ActionTypeId: 256 | Category: Deploy 257 | Owner: AWS 258 | Version: 1 259 | Provider: CloudFormation 260 | Configuration: 261 | ChangeSetName: sample-lambda-test 262 | ActionMode: CHANGE_SET_REPLACE 263 | StackName: sample-lambda-test 264 | Capabilities: CAPABILITY_NAMED_IAM 265 | TemplatePath: BuildOutput::packaged-template.yml 266 | #RoleArn: !Sub arn:aws:iam::${TestAccount}:role/cloudformationdeployer-role 267 | RoleArn: 268 | Fn::If: 269 | - AddCodeBuildResource 270 | - !Sub arn:aws:iam::${TestAccount}:role/cloudformationdeployer-role 271 | - !Ref AWS::NoValue 272 | InputArtifacts: 273 | - Name: BuildOutput 274 | RunOrder: 1 275 | #RoleArn: !Sub arn:aws:iam::${TestAccount}:role/ToolsAcctCodePipelineCloudFormationRole 276 | RoleArn: 277 | Fn::If: 278 | - AddCodeBuildResource 279 | - !Sub arn:aws:iam::${TestAccount}:role/ToolsAcctCodePipelineCloudFormationRole 280 | - !Ref AWS::NoValue 281 | - Name: DeployChangeSetTest 282 | ActionTypeId: 283 | Category: Deploy 284 | Owner: AWS 285 | Version: 1 286 | Provider: CloudFormation 287 | Configuration: 288 | ChangeSetName: sample-lambda-test 289 | ActionMode: CHANGE_SET_EXECUTE 290 | StackName: sample-lambda-test 291 | #RoleArn: !Sub arn:aws:iam::${TestAccount}:role/cloudformationdeployer-role 292 | RoleArn: 293 | Fn::If: 294 | - AddCodeBuildResource 295 | - !Sub arn:aws:iam::${TestAccount}:role/cloudformationdeployer-role 296 | - !Ref AWS::NoValue 297 | InputArtifacts: 298 | - Name: BuildOutput 299 | RunOrder: 2 300 | #RoleArn: !Sub arn:aws:iam::${TestAccount}:role/ToolsAcctCodePipelineCloudFormationRole 301 | RoleArn: 302 | Fn::If: 303 | - AddCodeBuildResource 304 | - !Sub arn:aws:iam::${TestAccount}:role/ToolsAcctCodePipelineCloudFormationRole 305 | - !Ref AWS::NoValue 306 | - Name: ApproveDeployProd 307 | Actions: 308 | - 309 | Name: ApproveDeployProd 310 | ActionTypeId: 311 | Category: Approval 312 | Owner: AWS 313 | Version: 1 314 | Provider: Manual 315 | Configuration: 316 | CustomData: "Log into the TEST account and test out your changes before approving." 317 | - Name: DeployToProduction 318 | Actions: 319 | - Name: CreateChangeSetProd 320 | ActionTypeId: 321 | Category: Deploy 322 | Owner: AWS 323 | Version: 1 324 | Provider: CloudFormation 325 | Configuration: 326 | ChangeSetName: sample-lambda-production 327 | ActionMode: CHANGE_SET_REPLACE 328 | StackName: sample-lambda-production 329 | Capabilities: CAPABILITY_NAMED_IAM 330 | TemplatePath: BuildOutput::packaged-template.yml 331 | #RoleArn: !Sub arn:aws:iam::${ProductionAccount}:role/cloudformationdeployer-role 332 | RoleArn: 333 | Fn::If: 334 | - AddCodeBuildResource 335 | - !Sub arn:aws:iam::${ProductionAccount}:role/cloudformationdeployer-role 336 | - !Ref AWS::NoValue 337 | InputArtifacts: 338 | - Name: BuildOutput 339 | RunOrder: 1 340 | #RoleArn: !Sub arn:aws:iam::${ProductionAccount}:role/ToolsAcctCodePipelineCloudFormationRole 341 | RoleArn: 342 | Fn::If: 343 | - AddCodeBuildResource 344 | - !Sub arn:aws:iam::${ProductionAccount}:role/ToolsAcctCodePipelineCloudFormationRole 345 | - !Ref AWS::NoValue 346 | - Name: DeployChangeSetProd 347 | ActionTypeId: 348 | Category: Deploy 349 | Owner: AWS 350 | Version: 1 351 | Provider: CloudFormation 352 | Configuration: 353 | ChangeSetName: sample-lambda-production 354 | ActionMode: CHANGE_SET_EXECUTE 355 | StackName: sample-lambda-production 356 | #RoleArn: !Sub arn:aws:iam::${ProductionAccount}:role/cloudformationdeployer-role 357 | RoleArn: 358 | Fn::If: 359 | - AddCodeBuildResource 360 | - !Sub arn:aws:iam::${ProductionAccount}:role/cloudformationdeployer-role 361 | - !Ref AWS::NoValue 362 | InputArtifacts: 363 | - Name: BuildOutput 364 | RunOrder: 2 365 | #RoleArn: !Sub arn:aws:iam::${ProductionAccount}:role/ToolsAcctCodePipelineCloudFormationRole 366 | RoleArn: 367 | Fn::If: 368 | - AddCodeBuildResource 369 | - !Sub arn:aws:iam::${ProductionAccount}:role/ToolsAcctCodePipelineCloudFormationRole 370 | - !Ref AWS::NoValue 371 | 372 | ArtifactStore: 373 | Type: S3 374 | Location: !Ref S3Bucket 375 | EncryptionKey: 376 | Id: !Ref CMKARN 377 | Type: KMS 378 | S3BucketPolicy: 379 | Type: AWS::S3::BucketPolicy 380 | Properties: 381 | Bucket: !Ref S3Bucket 382 | PolicyDocument: 383 | Statement: 384 | - 385 | Action: 386 | - s3:* 387 | Effect: Allow 388 | Resource: 389 | - !Sub arn:aws:s3:::${S3Bucket} 390 | - !Sub arn:aws:s3:::${S3Bucket}/* 391 | Principal: 392 | AWS: 393 | - !Sub arn:aws:iam::${DevAccount}:role/ToolsAcctCodePipelineCodeCommitRole 394 | - !Sub arn:aws:iam::${TestAccount}:role/ToolsAcctCodePipelineCloudFormationRole 395 | - !Sub arn:aws:iam::${TestAccount}:role/cloudformationdeployer-role 396 | - !Sub arn:aws:iam::${ProductionAccount}:role/ToolsAcctCodePipelineCloudFormationRole 397 | - !Sub arn:aws:iam::${ProductionAccount}:role/cloudformationdeployer-role 398 | - !GetAtt [BuildProjectRole,Arn] 399 | --------------------------------------------------------------------------------