Skip to content

Commit 3eb97d8

Browse files
committed
CloudFormation stacks for creating a Postgres instance, deploying a Spring Boot app and connecting the app to the database.
1 parent 27e1428 commit 3eb97d8

File tree

9 files changed

+572
-1
lines changed

9 files changed

+572
-1
lines changed

aws/aws-rds-hello-world/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Use the image instead of your real application to test AWS CloudFormation stacks
1616
4. Configure your deployment in a way that Docker will pass the coordinates to your RDS database as environment variables, equivalent to this command:
1717
```
1818
docker run \
19-
-e SPRING_DATASOURCE_URL=jdbc:postgresql://<RDS-ENDPOINT>:5432/postgres \
19+
-e SPRING_DATASOURCE_URL=':'<RDS-ENDPOINT>:5432/postgres \
2020
-e SPRING_DATASOURCE_USERNAME=<USERNAME> \
2121
-e SPRING_DATASOURCE_PASSWORD=<PASSWORD> \
2222
-p 8080:8080 reflectoring/aws-rds-hello-world
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Overview
2+
3+
![ECS in two public subnets](ecs-in-two-public-subnets.svg)
4+
5+
# Companion Blog Post
6+
7+
[The AWS Journey Part 2: Deploying a Docker image from the Command Line with CloudFormation](https://reflectoring.io/aws-cloudformation-deploy-docker-image/)
8+

aws/cloudformation/ecs-in-two-public-subnets/service.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ Resources:
7676
!Join [':', [!Ref 'StackName', 'PublicListener']]
7777
Priority: 1
7878

79+
LogGroup:
80+
Type: AWS::Logs::LogGroup
81+
Properties:
82+
LogGroupName: !Ref 'ServiceName'
83+
RetentionInDays: 1
84+
7985
TaskDefinition:
8086
Type: AWS::ECS::TaskDefinition
8187
Properties:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Overview
2+
3+
![RDS in private subnet](rds-in-private-subnet.svg)
4+
5+
# Companion Blog Post
6+
7+
TO DO
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Description: A stack that creates an RDS instance and places it into two subnets
3+
Parameters:
4+
NetworkStackName:
5+
Type: String
6+
Description: The name of the networking stack that this stack will build upon.
7+
DBInstanceClass:
8+
Type: String
9+
Description: The ID of the second subnet to place the RDS instance into.
10+
Default: 'db.t2.micro'
11+
DBName:
12+
Type: String
13+
Description: The name of the database that is created within the PostgreSQL instance.
14+
DBUsername:
15+
Type: String
16+
Description: The master user name for the PostgreSQL instance.
17+
Resources:
18+
19+
Secret:
20+
Type: "AWS::SecretsManager::Secret"
21+
Properties:
22+
Name: !Ref 'DBUsername'
23+
GenerateSecretString:
24+
# This will generate a JSON object with the keys "username" and password.
25+
SecretStringTemplate: !Join ['', ['{"username": "', !Ref 'DBUsername' ,'"}']]
26+
GenerateStringKey: "password"
27+
PasswordLength: 32
28+
29+
DBSubnetGroup:
30+
Type: AWS::RDS::DBSubnetGroup
31+
Properties:
32+
DBSubnetGroupDescription: Subnet group for the RDS instance
33+
DBSubnetGroupName: DBSubnetGroup
34+
SubnetIds:
35+
- Fn::ImportValue:
36+
!Join [':', [!Ref 'NetworkStackName', 'PrivateSubnetOne']]
37+
- Fn::ImportValue:
38+
!Join [':', [!Ref 'NetworkStackName', 'PrivateSubnetTwo']]
39+
40+
PostgresInstance:
41+
Type: AWS::RDS::DBInstance
42+
Properties:
43+
AllocatedStorage: 20
44+
AvailabilityZone:
45+
Fn::Select:
46+
- 0
47+
- Fn::GetAZs: {Ref: 'AWS::Region'}
48+
DBInstanceClass: !Ref 'DBInstanceClass'
49+
DBName: !Ref 'DBName'
50+
DBSubnetGroupName: !Ref 'DBSubnetGroup'
51+
Engine: postgres
52+
EngineVersion: 11.5
53+
MasterUsername: !Ref 'DBUsername'
54+
MasterUserPassword: !Join ['', ['{{resolve:secretsmanager:', !Ref Secret, ':SecretString:password}}' ]]
55+
PubliclyAccessible: false
56+
VPCSecurityGroups:
57+
- Fn::ImportValue:
58+
!Join [':', [!Ref 'NetworkStackName', 'DBSecurityGroupId']]
59+
60+
SecretRDSInstanceAttachment:
61+
Type: "AWS::SecretsManager::SecretTargetAttachment"
62+
Properties:
63+
SecretId: !Ref Secret
64+
TargetId: !Ref PostgresInstance
65+
TargetType: AWS::RDS::DBInstance
66+
67+
Outputs:
68+
EndpointAddress:
69+
Description: Address of the RDS endpoint.
70+
Value: !GetAtt 'PostgresInstance.Endpoint.Address'
71+
Export:
72+
Name: !Join [ ':', [ !Ref 'AWS::StackName', 'EndpointAddress' ] ]
73+
EndpointPort:
74+
Description: Port of the RDS endpoint.
75+
Value: !GetAtt 'PostgresInstance.Endpoint.Port'
76+
Export:
77+
Name: !Join [ ':', [ !Ref 'AWS::StackName', 'EndpointPort' ] ]
78+
Secret:
79+
Description: Reference to the secret containing the password to the database.
80+
Value: !Ref 'Secret'
81+
Export:
82+
Name: !Join [ ':', [ !Ref 'AWS::StackName', 'Secret' ] ]

0 commit comments

Comments
 (0)