Skip to content

Commit 1ac8919

Browse files
committed
add RDS IAAC
1 parent 131bf6a commit 1ac8919

File tree

3 files changed

+87
-4
lines changed

3 files changed

+87
-4
lines changed

infrastructure/app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33

44
import aws_cdk as cdk
55

6-
from lambda_s3_trigger.lambda_s3_trigger_stack import LambdaS3TriggerStack
6+
from lambda_s3_trigger.lambda_s3_trigger_stack import LambdaS3TriggerRDSStack
77

88

99
app = cdk.App()
10-
LambdaS3TriggerStack(app, "LambdaS3TriggerStack",
10+
LambdaS3TriggerRDSStack(app, "LambdaS3TriggerRDSStack",
1111
# If you don't specify 'env', this stack will be environment-agnostic.
1212
# Account/Region-dependent features and context lookups will not work,
1313
# but a single synthesized template can be deployed anywhere.
1414

1515
# Uncomment the next line to specialize this stack for the AWS Account
1616
# and Region that are implied by the current CLI configuration.
1717

18-
#env=cdk.Environment(account=os.getenv('CDK_DEFAULT_ACCOUNT'), region=os.getenv('CDK_DEFAULT_REGION')),
18+
env=cdk.Environment(account=os.getenv('CDK_DEFAULT_ACCOUNT'), region=os.getenv('CDK_DEFAULT_REGION')),
1919

2020
# Uncomment the next line if you know exactly what Account and Region you
2121
# want to deploy the stack to. */

infrastructure/cdk.context.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"vpc-provider:account=247477386084:filter.isDefault=true:region=us-east-1:returnAsymmetricSubnets=true": {
3+
"vpcId": "vpc-0e721c5275330dc9d",
4+
"vpcCidrBlock": "172.31.0.0/16",
5+
"availabilityZones": [],
6+
"subnetGroups": [
7+
{
8+
"name": "Public",
9+
"type": "Public",
10+
"subnets": [
11+
{
12+
"subnetId": "subnet-0aeeb6080e7f83eab",
13+
"cidr": "172.31.32.0/20",
14+
"availabilityZone": "us-east-1a",
15+
"routeTableId": "rtb-0d1890a6ff99d251a"
16+
},
17+
{
18+
"subnetId": "subnet-0adedc0e514144b28",
19+
"cidr": "172.31.0.0/20",
20+
"availabilityZone": "us-east-1b",
21+
"routeTableId": "rtb-0d1890a6ff99d251a"
22+
},
23+
{
24+
"subnetId": "subnet-07b9f80c3970e038c",
25+
"cidr": "172.31.80.0/20",
26+
"availabilityZone": "us-east-1c",
27+
"routeTableId": "rtb-0d1890a6ff99d251a"
28+
},
29+
{
30+
"subnetId": "subnet-08bd965fe1739672b",
31+
"cidr": "172.31.16.0/20",
32+
"availabilityZone": "us-east-1d",
33+
"routeTableId": "rtb-0d1890a6ff99d251a"
34+
},
35+
{
36+
"subnetId": "subnet-0a4b57d447c3f30e9",
37+
"cidr": "172.31.48.0/20",
38+
"availabilityZone": "us-east-1e",
39+
"routeTableId": "rtb-0d1890a6ff99d251a"
40+
},
41+
{
42+
"subnetId": "subnet-0ade512f1ba43086c",
43+
"cidr": "172.31.64.0/20",
44+
"availabilityZone": "us-east-1f",
45+
"routeTableId": "rtb-0d1890a6ff99d251a"
46+
}
47+
]
48+
}
49+
]
50+
}
51+
}

infrastructure/lambda_s3_trigger/lambda_s3_trigger_stack.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,50 @@
11
from aws_cdk import (
22
aws_lambda as _lambda,
3+
aws_rds as rds,
34
aws_s3 as _s3,
45
aws_s3_notifications,
6+
aws_ec2 as ec2,
57
Duration,
68
Stack
79
)
810

911
from constructs import Construct
1012

11-
class LambdaS3TriggerStack(Stack):
13+
class LambdaS3TriggerRDSStack(Stack):
1214

1315
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
1416
super().__init__(scope, id, **kwargs)
1517

18+
# Retrieve the default VPC for the account
19+
vpc = ec2.Vpc.from_lookup(self, "MyVPC", is_default=True)
20+
21+
sg = ec2.SecurityGroup(self, 'mydb-sg',
22+
vpc=vpc,
23+
allow_all_outbound=True,
24+
)
25+
26+
# Allow inbound traffic on ports 3306, 80, and 443
27+
sg.add_ingress_rule(ec2.Peer.any_ipv4(), ec2.Port.tcp(3306))
28+
sg.add_ingress_rule(ec2.Peer.any_ipv4(), ec2.Port.tcp(80))
29+
sg.add_ingress_rule(ec2.Peer.any_ipv4(), ec2.Port.tcp(443))
30+
31+
# Create an Amazon RDS for MySQL database
32+
db = rds.DatabaseInstance(
33+
self, "mydb",
34+
engine=rds.DatabaseInstanceEngine.MYSQL,
35+
instance_type=ec2.InstanceType.of(
36+
instance_class=ec2.InstanceClass.T3,
37+
instance_size=ec2.InstanceSize.MICRO
38+
),
39+
vpc = vpc,
40+
vpc_subnets=ec2.SubnetSelection(
41+
subnet_type=ec2.SubnetType.PUBLIC,
42+
),
43+
security_groups=[sg],
44+
database_name='mydb'
45+
)
46+
47+
1648
# create s3 bucket
1749
s3 = _s3.Bucket(self, "s3bucket")
1850

0 commit comments

Comments
 (0)