Skip to content

Commit 0a05154

Browse files
IaC - AWS CloudFormation example
1 parent 7ac9802 commit 0a05154

4 files changed

Lines changed: 157 additions & 0 deletions

File tree

IaC/README.txt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Commands to test:
2+
------------------
3+
4+
Create s3 bucket with the name <BUCKET_NAME>.
5+
You can use aws cli or web console for this step.
6+
7+
Login to your AWS account through the web console and create a SSK key pair named "assignment4".
8+
Download the .pem file (assignment4.pem) on your machine.
9+
This SSH key pair will help you to login to the created VM.
10+
The public key will be injected into the VM, and the private key will be
11+
available on your machine (this is the .pem file).
12+
13+
14+
1) Copy files to s3 bucket:
15+
aws s3 cp application.py s3://<BUCKET_NAME>/application.py
16+
aws s3 cp requirements.txt s3://<BUCKET_NAME>/requirements.txt
17+
18+
2) Create CF stack:
19+
aws cloudformation create-stack \
20+
--stack-name assignment4 \
21+
--template-body file://cf_template.yaml \
22+
--parameters \
23+
ParameterKey=KeyName,ParameterValue=assignment4 \
24+
ParameterKey=S3BucketName,ParameterValue=<BUCKET_NAME> \
25+
ParameterKey=EnvValue,ParameterValue="Testing CloudFormation" \
26+
--capabilities CAPABILITY_NAMED_IAM
27+
28+
3) Wait for stack creation:
29+
aws cloudformation wait stack-create-complete \
30+
--stack-name assignment4
31+
32+
4) Get Public IP address:
33+
aws cloudformation describe-stacks \
34+
--stack-name assignment4 \
35+
--query "Stacks[0].Outputs[?OutputKey=='PublicIP'].OutputValue" \
36+
--output text
37+
38+
5) Verify:
39+
curl http://<PUBLIC_IP>
40+
41+
6) Delete stack;
42+
aws cloudformation delete-stack \
43+
--stack-name assignment4
44+
45+
7) Ssh into the VM:
46+
ssh -i assignment4.pem ubuntu@<PUBLIC_IP>
47+
48+
8) Check logs:
49+
cat /var/log/cloud-init-output.log
50+
cat /home/ubuntu/app/app.log
51+

IaC/application.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from flask import Flask
2+
import os
3+
4+
app = Flask(__name__)
5+
6+
@app.route("/")
7+
def home():
8+
# Requirement 1
9+
# read env var MY_ENV_VAR
10+
# return its value
11+
return ""
12+
13+
if __name__ == "__main__":
14+
app.run(host="0.0.0.0", port=80)

IaC/cf_template.yaml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
AWSTemplateFormatVersion: "2010-09-09"
2+
Description: EC2 with Flask app pulling code from S3
3+
4+
Parameters:
5+
InstanceType:
6+
Type: String
7+
Default: t3.micro
8+
9+
KeyName:
10+
Type: AWS::EC2::KeyPair::KeyName
11+
Description: SSH Key
12+
13+
# Requirement 2.1
14+
#
15+
# Requirement 2.2
16+
17+
Resources:
18+
19+
EC2Role:
20+
Type: AWS::IAM::Role
21+
Properties:
22+
AssumeRolePolicyDocument:
23+
Version: "2012-10-17"
24+
Statement:
25+
- Effect: Allow
26+
Principal:
27+
Service: ec2.amazonaws.com
28+
Action: sts:AssumeRole
29+
30+
Policies:
31+
- PolicyName: S3ReadAccess
32+
PolicyDocument:
33+
Version: "2012-10-17"
34+
Statement:
35+
- Effect: Allow
36+
Action:
37+
- s3:GetObject
38+
Resource: !Sub arn:aws:s3:::${S3BucketName}/*
39+
40+
InstanceProfile:
41+
Type: AWS::IAM::InstanceProfile
42+
Properties:
43+
Roles:
44+
- !Ref EC2Role
45+
46+
SecurityGroup:
47+
Type: AWS::EC2::SecurityGroup
48+
Properties:
49+
GroupDescription: Allow HTTP + SSH
50+
SecurityGroupIngress:
51+
- IpProtocol: tcp
52+
FromPort: 22
53+
ToPort: 22
54+
CidrIp: 0.0.0.0/0
55+
# Requirement 2.3
56+
57+
EC2Instance:
58+
Type: AWS::EC2::Instance
59+
Properties:
60+
InstanceType: !Ref InstanceType
61+
KeyName: !Ref KeyName
62+
63+
# Requirement 2.4
64+
ImageId: "" # Ubuntu 22.04 (update for us-west-2 region)
65+
66+
IamInstanceProfile: !Ref InstanceProfile
67+
SecurityGroupIds:
68+
- !Ref SecurityGroup
69+
70+
UserData:
71+
Fn::Base64: !Sub |
72+
#!/bin/bash
73+
apt update -y
74+
apt install -y python3 python3-pip awscli
75+
76+
# Requirement 2.5.1
77+
78+
# Requirement 2.5.2
79+
80+
# Requirement 2.5.3
81+
82+
# Set environment variable
83+
# Requirement 2.5.4
84+
85+
# Run app
86+
nohup python3 application.py > app.log 2>&1 &
87+
88+
Outputs:
89+
PublicIP:
90+
Description: Access the Flask app
91+
Value: !GetAtt EC2Instance.PublicIp

IaC/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flask

0 commit comments

Comments
 (0)