Skip to content

Commit 1b9d327

Browse files
Add CloudFormation deployment file
1 parent d0791a8 commit 1b9d327

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

deploy-infra.sh

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
STACK_NAME=awsbootstrap
4+
REGION=eu-central-1
5+
CLI_PROFILE=awsbootstrap
6+
EC2_INSTANCE_TYPE=t2.micro
7+
8+
# Deploy the CloudFormation template
9+
10+
aws cloudformation deploy \
11+
--region $REGION \
12+
--profile $CLI_PROFILE \
13+
--stack-name $STACK_NAME \
14+
--template-file main.yml \
15+
--no-fail-on-empty-changeset \
16+
--capabilities CAPABILITY_NAMED_IAM \
17+
--parameter-overrides \
18+
EC2InstanceType=$EC2_INSTANCE_TYPE
19+
20+
# If the deploy succeeded, show the DNS name of the created instance
21+
if [ $? -eq 0 ]; then
22+
aws cloudformation list-exports \
23+
--profile awsbootstrap \
24+
--query "Exports[?Name=='InstanceEndpoint'].Value"
25+
fi

main.yml

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
AWSTemplateFormatVersion: 2010-09-09
2+
3+
Parameters:
4+
EC2InstanceType:
5+
Type: String
6+
EC2AMI:
7+
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
8+
Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
9+
10+
Resources:
11+
SecurityGroup:
12+
Type: AWS::EC2::SecurityGroup
13+
Properties:
14+
GroupDescription: !Sub 'Internal Security Group for ${AWS::StackName}'
15+
SecurityGroupIngress:
16+
- IpProtocol: tcp
17+
FromPort: 8080
18+
ToPort: 8080
19+
CidrIp: 0.0.0.0/0
20+
- IpProtocol: tcp
21+
FromPort: 22
22+
ToPort: 22
23+
CidrIp: 0.0.0.0/0
24+
Tags:
25+
- Key: Name
26+
Value: !Ref AWS::StackName
27+
28+
InstanceRole:
29+
Type: "AWS::IAM::Role"
30+
Properties:
31+
AssumeRolePolicyDocument:
32+
Version: "2012-10-17"
33+
Statement:
34+
Effect: Allow
35+
Principal:
36+
Service:
37+
- "ec2.amazonaws.com"
38+
Action: sts:AssumeRole
39+
ManagedPolicyArns:
40+
- arn:aws:iam::aws:policy/CloudWatchFullAccess
41+
Tags:
42+
- Key: Name
43+
Value: !Ref AWS::StackName
44+
45+
InstanceProfile:
46+
Type: "AWS::IAM::InstanceProfile"
47+
Properties:
48+
Roles:
49+
- Ref: InstanceRole
50+
51+
Instance:
52+
Type: AWS::EC2::Instance
53+
CreationPolicy:
54+
ResourceSignal:
55+
Timeout: PT30M
56+
Count: 1
57+
Metadata:
58+
AWS::CloudFormation::Init:
59+
config:
60+
packages:
61+
yum:
62+
wget: []
63+
unzip: []
64+
Properties:
65+
ImageId: !Ref EC2AMI
66+
InstanceType: !Ref EC2InstanceType
67+
IamInstanceProfile: !Ref InstanceProfile
68+
Monitoring: true
69+
SecurityGroupIds:
70+
- !GetAtt SecurityGroup.GroupId
71+
UserData:
72+
Fn::Base64: !Sub |
73+
#!/bin/bash -xe
74+
75+
# send script output to /tmp so we can debug boot failures
76+
exec > /tmp/userdata.log 2>&1
77+
78+
# Update all packages
79+
yum -y update
80+
81+
# Get latest cfn scripts; https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/best-practices.html#cfninit
82+
yum install -y aws-cfn-bootstrap
83+
84+
# Have CloudFormation install any files and packages from the metadata
85+
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --region ${AWS::Region} --resource Instance
86+
87+
cat > /tmp/install_script.sh << EOF
88+
# START
89+
echo "Setting up NodeJS Environment"
90+
curl https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
91+
92+
# Dot source the files to ensure that variables are available within the current shell
93+
. /home/ec2-user/.nvm/nvm.sh
94+
. /home/ec2-user/.bashrc
95+
96+
# Install NVM, NPM, Node.JS
97+
nvm alias default v12.7.0
98+
nvm install v12.7.0
99+
nvm use v12.7.0
100+
101+
# Download latest code, unzip it into /home/ec2-user/app
102+
wget https://github.com/<username>/aws-bootstrap/archive/master.zip
103+
unzip master.zip
104+
mv aws-bootstrap-master app
105+
106+
# Create log directory
107+
mkdir -p /home/ec2-user/app/logs
108+
109+
# Run server
110+
cd app
111+
npm install
112+
npm start
113+
EOF
114+
115+
chown ec2-user:ec2-user /tmp/install_script.sh && chmod a+x /tmp/install_script.sh
116+
sleep 1; su - ec2-user -c "/tmp/install_script.sh
117+
118+
# Signal to CloudFormation that the instance is ready
119+
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --region ${AWS::Region} --resource Instance
120+
Tags:
121+
- Key: Name
122+
Value: !Ref AWS::StackName
123+
124+
Outputs:
125+
InstanceEndpoint:
126+
Description: The DNS name for the created instance
127+
Value: !Sub "http://${Instance.PublicDnsName}:8080"
128+
Export:
129+
Name: InstanceEndpoint

0 commit comments

Comments
 (0)