Skip to content

Commit c62a1fb

Browse files
author
Prakash Palanisamy
committed
Added templates and buildspec files
1 parent bfb4817 commit c62a1fb

20 files changed

+1779
-2
lines changed

LICENSE.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Amazon Software License
2+
3+
This Amazon Software License (“License”) governs your use, reproduction, and distribution of the accompanying software as specified below.
4+
5+
1. Definitions
6+
7+
“Licensor” means any person or entity that distributes its Work.
8+
9+
“Software” means the original work of authorship made available under this License.
10+
11+
“Work” means the Software and any additions to or derivative works of the Software that are made available under this License.
12+
13+
The terms “reproduce,” “reproduction,” “derivative works,” and “distribution” have the meaning as provided under U.S. copyright law; provided, however, that for the purposes of this License, derivative works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work.
14+
15+
Works, including the Software, are “made available” under this License by including in or with the Work either (a) a copyright notice referencing the applicability of this License to the Work, or (b) a copy of this License.
16+
17+
2. License Grants
18+
19+
2.1 Copyright Grant. Subject to the terms and conditions of this License, each Licensor grants to you a perpetual, worldwide, non-exclusive, royalty-free, copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, sublicense and distribute its Work and any resulting derivative works in any form.
20+
21+
2.2 Patent Grant. Subject to the terms and conditions of this License, each Licensor grants to you a perpetual, worldwide, non-exclusive, royalty-free patent license to make, have made, use, sell, offer for sale, import, and otherwise transfer its Work, in whole or in part. The foregoing license applies only to the patent claims licensable by Licensor that would be infringed by Licensor’s Work (or portion thereof) individually and excluding any combinations with any other materials or technology.
22+
23+
3. Limitations
24+
25+
3.1 Redistribution. You may reproduce or distribute the Work only if (a) you do so under this License, (b) you include a complete copy of this License with your distribution, and (c) you retain without modification any copyright, patent, trademark, or attribution notices that are present in the Work.
26+
27+
3.2 Derivative Works. You may specify that additional or different terms apply to the use, reproduction, and distribution of your derivative works of the Work (“Your Terms”) only if (a) Your Terms provide that the use limitation in Section 3.3 applies to your derivative works, and (b) you identify the specific derivative works that are subject to Your Terms. Notwithstanding Your Terms, this License (including the redistribution requirements in Section 3.1) will continue to apply to the Work itself.
28+
29+
3.3 Use Limitation. The Work and any derivative works thereof only may be used or intended for use with the web services, computing platforms or applications provided by Amazon.com, Inc. or its affiliates, including Amazon Web Services, Inc.
30+
31+
3.4 Patent Claims. If you bring or threaten to bring a patent claim against any Licensor (including any claim, cross-claim or counterclaim in a lawsuit) to enforce any patents that you allege are infringed by any Work, then your rights under this License from such Licensor (including the grants in Sections 2.1 and 2.2) will terminate immediately.
32+
33+
3.5 Trademarks. This License does not grant any rights to use any Licensor’s or its affiliates’ names, logos, or trademarks, except as necessary to reproduce the notices described in this License.
34+
35+
3.6 Termination. If you violate any term of this License, then your rights under this License (including the grants in Sections 2.1 and 2.2) will terminate immediately.
36+
37+
4. Disclaimer of Warranty.
38+
39+
THE WORK IS PROVIDED “AS IS” WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WARRANTIES OR CONDITIONS OF M ERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE OR NON-INFRINGEMENT. YOU BEAR THE RISK OF UNDERTAKING ANY ACTIVITIES UNDER THIS LICENSE. SOME STATES’ CONSUMER LAWS DO NOT ALLOW EXCLUSION OF AN IMPLIED WARRANTY, SO THIS DISCLAIMER MAY NOT APPLY TO YOU.
40+
41+
5. Limitation of Liability.
42+
43+
EXCEPT AS PROHIBITED BY APPLICABLE LAW, IN NO EVENT AND UNDER NO LEGAL THEORY, WHETHER IN TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE SHALL ANY LICENSOR BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF OR RELATED TO THIS LICENSE, THE USE OR INABILITY TO USE THE WORK (INCLUDING BUT NOT LIMITED TO LOSS OF GOODWILL, BUSINESS INTERRUPTION, LOST PROFITS OR DATA, COMPUTER FAILURE OR MALFUNCTION, OR ANY OTHER COMM ERCIAL DAMAGES OR LOSSES), EVEN IF THE LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
44+
45+
Effective Date – April 18, 2008 © 2008 Amazon.com, Inc. or its affiliates. All rights reserved.

NOTICE.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2016-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
Licensed under the Amazon Software License (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/asl/
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, express or implied. See the License for the specific language governing permissions and limitations under the License.

README.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,62 @@
1-
# codepipeline-nested-cfn
2-
CloudFormation templates, CodeBuild build specification & Python scripts to perform unit tests of a nested CloudFormation template.
1+
# CodePipeline-Nested-CFN
2+
3+
This repo contains the CloudFormtaion template which will create a CodePipeline containing multiple stages starting from CodeCommit as source stage, followed by build using CodeBuild, launch test stack, test using CodeBuild, proceed with UAT deployment and wait for manual approval. Once approved it proceed to production stage where it creates a CloudFormation ChangeSet for production stack and wait for approval, once approved it will execute the ChangeSet in production stack.
4+
5+
![CodePipeline Design](images/Pipeline_Design.png)
6+
7+
Let's start by creating the repositories and enabling Continuous Delivery pipeline for nested CFN.
8+
9+
## Step 1:
10+
11+
### Create base VPC Stack
12+
In the [cfn-nested-repo](cfn-nested-repo/) directory there are multiple YAML (*CloudFormation Templates*) & JSON (*CloudFormation Configuration*) files.
13+
14+
**[vpc-stack.yml](cfn-nested-repo/vpc-stack.yml):** is the CloudFormation template to create the base VPC, Subnets, NAT Gateways, etc which will be used.
15+
**[vpc-params.json](cfn-nested-repo/vpc-params.json):** is the parameters file which contains the parameter values for the CFN template. Update the *ProdApprovalEmail & UATApprovalEmail* values to provide the appropriate email address.
16+
17+
Go to `cfn-nested-repo` directory and execute the following AWS CLI command to create CloudFormation stack.
18+
19+
```bash
20+
cd cfn-nested-repo
21+
aws cloudformation create-stack --stack-name NestedCFN-BaseStack --template-body file://vpc-stack.yml --parameters file://vpc-params.json
22+
```
23+
24+
## Step 2:
25+
26+
### Update CloudFormation parameters configuration files
27+
In the [cfn-nested-repo](cfn-nested-repo/) directory there are following 3 JSON (*CloudFormation Configuration*) files.
28+
29+
**[config-test.json](cfn-nested-repo/config-test.json):** - CloudFormation parameter configuration file for test stack
30+
**[config-uat.json](cfn-nested-repo/config-uat.json):** - CloudFormation parameter configuration file for UAT stack
31+
**[config-prod.json](cfn-nested-repo/config-prod.json):** - CloudFormation parameter configuration file for Prod stack
32+
33+
Update these 3 configuration files with appropriate values for *VPCID, PrivateSubnet1, PrivateSubnet2, PublicSubnet1, PublicSubnet2, S3BucketName & DBSubnetGroup* based on the values in the output section of the base VPC stack created in Step 1. Update *KeyPair* value with an existing key pair or create a new key pair and use it.
34+
35+
## Step 3:
36+
37+
### Creating CodeCommit repositories
38+
Create two CodeCommit repositories as mentioned below.
39+
40+
```bash
41+
aws codecommit create-repository --repository-name cfn-nested-repo --repository-description "Repository for CloudFormation templates"
42+
43+
aws codecommit create-repository --repository-name validate-resources --repository-description "Repository for unit testing CloudFormation resources"
44+
```
45+
46+
Once the repositories are create, clone those repositories and upload the content of directories `cfn-nested-repo` & `validate-resources` in their corresponding repositories.
47+
48+
## Step 4:
49+
50+
### Creating CodePipeline using CloudFormation
51+
52+
Update the **[codepipeline-cfn-codebuild.json](codepipeline-cfn-codebuild.json)** file with the appropriate values for *ArtifactStoreS3Location, UATTopic & ProdTopic* based on the values from output section of main stack created in Step 1 and update the values for *CFNTemplateRepoName & ValidateResourcesRepoName* with appropriate values based on the repositories created in Step 3.
53+
54+
Once the configuration file has been updated, execute the following command to create the CloudFormation stack which will create the required CodePipeline.
55+
56+
```bash
57+
aws cloudformation create-stack --stack-name NestedCFN-CodePipeline --template-body file://codepipeline-cfn-codebuild.yml --parameters file://codepipeline-cfn-codebuild.json --capabilities CAPABILITY_NAMED_IAM
58+
```
59+
60+
Once the CloudFormation successfully creates the stack, it would have created a CodePipeline with similar stages as shown below.
61+
62+
![CodePipeline Stages](images/Pipeline_Flow.png)

cfn-nested-repo/buildspec.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
version: 0.1
2+
3+
environment_variables:
4+
plaintext:
5+
CHILD_TEMPLATES: |
6+
security-stack.yml
7+
server-stack.yml
8+
database-stack.yml
9+
TEMPLATE_FILES: |
10+
master-stack.yml
11+
security-stack.yml
12+
server-stack.yml
13+
database-stack.yml
14+
CONFIG_FILES: |
15+
config-prod.json
16+
config-test.json
17+
config-uat.json
18+
19+
phases:
20+
install:
21+
commands:
22+
npm install jsonlint -g
23+
pre_build:
24+
commands:
25+
- echo "Validating CFN templates"
26+
- |
27+
for cfn_template in $TEMPLATE_FILES; do
28+
echo "Validating CloudFormation template file $cfn_template"
29+
aws cloudformation validate-template --template-body file://$cfn_template
30+
done
31+
- |
32+
for conf in $CONFIG_FILES; do
33+
echo "Validating CFN parameters config file $conf"
34+
jsonlint -q $conf
35+
done
36+
build:
37+
commands:
38+
- echo "Copying child stack templates to S3"
39+
- |
40+
for child_template in $CHILD_TEMPLATES; do
41+
if [ "X$TEMPLATE_PREFIX" = "X" ]; then
42+
aws s3 cp "$child_template" "s3://$TEMPLATE_BUCKET/$child_template"
43+
else
44+
aws s3 cp "$child_template" "s3://$TEMPLATE_BUCKET/$TEMPLATE_PREFIX/$child_template"
45+
fi
46+
done
47+
- echo "Updating template configurtion files to use the appropriate values"
48+
- |
49+
for conf in $CONFIG_FILES; do
50+
if [ "X$TEMPLATE_PREFIX" = "X" ]; then
51+
echo "Replacing \"TEMPLATE_PATH_PLACEHOLDER\" for \"$TEMPLATE_BUCKET\" in $conf"
52+
sed -i -e "s/TEMPLATE_PATH_PLACEHOLDER/$TEMPLATE_BUCKET/" $conf
53+
else
54+
echo "Replacing \"TEMPLATE_PATH_PLACEHOLDER\" for \"$TEMPLATE_BUCKET/$TEMPLATE_PREFIX\" in $conf"
55+
sed -i -e "s/TEMPLATE_PATH_PLACEHOLDER/$TEMPLATE_BUCKET\/$TEMPLATE_PREFIX/" $conf
56+
fi
57+
done
58+
59+
artifacts:
60+
files:
61+
- master-stack.yml
62+
- config-*.json

cfn-nested-repo/config-prod.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"Parameters" : {
3+
"TemplatePath": "TEMPLATE_PATH_PLACEHOLDER",
4+
"VPCID": "vpc-a9b8c7d0",
5+
"PrivateSubnet1": "subnet-abcd2226",
6+
"PrivateSubnet2": "subnet-dcba2226",
7+
"PublicSubnet1": "subnet-abcd1bbb",
8+
"PublicSubnet2": "subnet-9069abcd",
9+
"S3BucketName": "com-amazon-demo-codepipeline-blog",
10+
"KeyPair": "codepipeline-demo",
11+
"AMIId": "ami-e5083683",
12+
"WebInstanceType": "t2.large",
13+
"WebMinSize": "1",
14+
"WebMaxSize": "2",
15+
"DBSubnetGroup": "basevpcstack-rdssubnetgroup-abcdf03cxyz5m",
16+
"DBUsername": "dbadmin",
17+
"DBPassword": "dbpAssw0rd",
18+
"DBInstanceType": "db.t2.small",
19+
"Environment": "prod"
20+
}
21+
}

cfn-nested-repo/config-test.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"Parameters" : {
3+
"TemplatePath": "TEMPLATE_PATH_PLACEHOLDER",
4+
"VPCID": "vpc-a9b8c7d0",
5+
"PrivateSubnet1": "subnet-abcd2226",
6+
"PrivateSubnet2": "subnet-dcba2226",
7+
"PublicSubnet1": "subnet-abcd1bbb",
8+
"PublicSubnet2": "subnet-9069abcd",
9+
"S3BucketName": "com-amazon-demo-codepipeline-blog",
10+
"KeyPair": "codepipeline-demo",
11+
"AMIId": "ami-e5083683",
12+
"WebInstanceType": "t2.large",
13+
"WebMinSize": "1",
14+
"WebMaxSize": "1",
15+
"DBSubnetGroup": "basevpcstack-rdssubnetgroup-abcdf03cxyz5m",
16+
"DBUsername": "dbadmin",
17+
"DBPassword": "dbpAssw0rd",
18+
"DBInstanceType": "db.t2.small",
19+
"Environment": "test"
20+
}
21+
}

cfn-nested-repo/config-uat.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"Parameters" : {
3+
"TemplatePath": "TEMPLATE_PATH_PLACEHOLDER",
4+
"VPCID": "vpc-a9b8c7d0",
5+
"PrivateSubnet1": "subnet-abcd2226",
6+
"PrivateSubnet2": "subnet-dcba2226",
7+
"PublicSubnet1": "subnet-abcd1bbb",
8+
"PublicSubnet2": "subnet-9069abcd",
9+
"S3BucketName": "com-amazon-demo-codepipeline-blog",
10+
"KeyPair": "codepipeline-demo",
11+
"AMIId": "ami-e5083683",
12+
"WebInstanceType": "t2.large",
13+
"WebMinSize": "1",
14+
"WebMaxSize": "1",
15+
"DBSubnetGroup": "basevpcstack-rdssubnetgroup-abcdf03cxyz5m",
16+
"DBUsername": "dbadmin",
17+
"DBPassword": "dbpAssw0rd",
18+
"DBInstanceType": "db.t2.small",
19+
"Environment": "uat"
20+
}
21+
}

cfn-nested-repo/database-stack.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
Description: Create database server
2+
3+
Parameters:
4+
DBSubnetGroup:
5+
Type: String
6+
Description: Enter a valid DB Subnet Group
7+
DBUsername:
8+
Type: String
9+
Description: Enter a valid Database master username
10+
MinLength: 1
11+
MaxLength: 16
12+
AllowedPattern: "[a-zA-Z][a-zA-Z0-9]*"
13+
DBPassword:
14+
Type: String
15+
Description: Enter a valid Database master password
16+
NoEcho: true
17+
MinLength: 1
18+
MaxLength: 41
19+
AllowedPattern: "[a-zA-Z0-9]*"
20+
DBServerSecurityGroup:
21+
Type: "AWS::EC2::SecurityGroup::Id"
22+
Description: Enter a valid security group ID for database
23+
DBInstanceType:
24+
Type: String
25+
Description: Enter one of the possible instance type for database
26+
AllowedValues:
27+
- db.t2.micro
28+
- db.t2.small
29+
- db.t2.medium
30+
- db.t2.large
31+
Environment:
32+
Type: String
33+
Description: Select the appropriate environment
34+
AllowedValues:
35+
- dev
36+
- test
37+
- uat
38+
- prod
39+
40+
Resources:
41+
DBServer:
42+
Type: "AWS::RDS::DBInstance"
43+
Properties:
44+
AllocatedStorage: 50
45+
AllowMajorVersionUpgrade: true
46+
AutoMinorVersionUpgrade: true
47+
BackupRetentionPeriod: 0
48+
DBInstanceClass:
49+
Ref: DBInstanceType
50+
DBSubnetGroupName:
51+
Ref: DBSubnetGroup
52+
Engine: mysql
53+
EngineVersion: 5.7.11
54+
MasterUsername:
55+
Ref: DBUsername
56+
MasterUserPassword:
57+
Ref: DBPassword
58+
MultiAZ: false
59+
PubliclyAccessible: false
60+
StorageType: gp2
61+
VPCSecurityGroups:
62+
- Ref: DBServerSecurityGroup
63+
Tags:
64+
- Key: Name
65+
Value:
66+
Fn::Sub: DBServer-${Environment}
67+
68+
Outputs:
69+
DBEndpoint:
70+
Description: "URL endpoint of database"
71+
Value:
72+
Fn::GetAtt: [ DBServer, Endpoint.Address ]

0 commit comments

Comments
 (0)