-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.yaml
More file actions
189 lines (187 loc) · 5.96 KB
/
template.yaml
File metadata and controls
189 lines (187 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: db access.
Resources:
# https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpc.html
MyVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.1.0.0/16
# https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet.html
MySubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref MyVPC
CidrBlock: 10.1.1.0/24
AvailabilityZone: ap-northeast-1a
MySubnet2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref MyVPC
CidrBlock: 10.1.2.0/24
AvailabilityZone: ap-northeast-1c
# https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-security-group.html
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allow http to client host
VpcId: !Ref MyVPC
SecurityGroupIngress:
- IpProtocol: -1
CidrIp: 0.0.0.0/0
# https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html
AccessDynamoDB:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
TableName: access_table
MyRDSSubnetGroup:
Type: AWS::RDS::DBSubnetGroup
Properties:
DBSubnetGroupDescription: description
SubnetIds:
- !Ref MySubnet1
- !Ref MySubnet2
# https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-properties-rds-database-instance.html
AccessRDS:
Type: AWS::RDS::DBInstance
Properties:
AllocatedStorage: '20'
DBInstanceClass: db.t2.micro
DBName : access_db
DBSubnetGroupName: !Ref MyRDSSubnetGroup
MasterUsername: rootuser
MasterUserPassword: rootuser00
Engine: MySQL
PubliclyAccessible: false
VPCSecurityGroups:
- !Ref InstanceSecurityGroup
DeletionPolicy: Delete
# https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-iam-role.html
DynamoAccessFunctionRole:
Type: AWS::IAM::Role
Properties:
Path: /
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: root
PolicyDocument:
Version: 2012-10-17
Statement:
- Resource: "*"
Effect: Allow
Action:
- logs:*
- dynamodb:*
# https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-iam-role.html
RdsAccessFunctionRole:
Type: AWS::IAM::Role
Properties:
Path: /
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: root
PolicyDocument:
Version: 2012-10-17
Statement:
- Resource: "*"
Effect: Allow
Action:
- logs:*
- rds:*
- ec2:CreateNetworkInterface
- ec2:DescribeNetworkInterfaces
- ec2:DeleteNetworkInterface
# https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/serverless-sam-cli-layers.html
DynamoAccessFunction:
Type: AWS::Serverless::Function
Properties:
Handler: function.handler
Runtime: ruby2.5
FunctionName: dynamo-access-func
CodeUri: ./dynamo-access-func
Timeout: 10
Role: !GetAtt DynamoAccessFunctionRole.Arn
Events:
GetResource:
Type: Api
Properties:
Path: /dynamo_access
Method: get
# https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/serverless-sam-cli-layers.html
RdsAccessFunction:
Type: AWS::Serverless::Function
Properties:
Handler: function.handler
Runtime: ruby2.5
FunctionName: rds-access-func
CodeUri: ./rds-access-func
Timeout: 10
Role: !GetAtt RdsAccessFunctionRole.Arn
Environment:
Variables:
RDS_ARN: !GetAtt AccessRDS.Endpoint.Address
Events:
GetResource:
Type: Api
Properties:
Path: /rds_access
Method: get
VpcConfig:
SecurityGroupIds:
- !Ref InstanceSecurityGroup
SubnetIds:
- !Ref MySubnet1
- !Ref MySubnet2
# https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/serverless-sam-cli-layers.html
RdsCreateTableFunction:
Type: AWS::Serverless::Function
Properties:
Handler: function.handler
Runtime: ruby2.5
FunctionName: rds-create-table-func
CodeUri: ./rds-create-table-func
Timeout: 10
Role: !GetAtt RdsAccessFunctionRole.Arn
Environment:
Variables:
RDS_ARN: !GetAtt AccessRDS.Endpoint.Address
Events:
GetResource:
Type: Api
Properties:
Path: /rds_create_table
Method: get
VpcConfig:
SecurityGroupIds:
- !Ref InstanceSecurityGroup
SubnetIds:
- !Ref MySubnet1
- !Ref MySubnet2
Outputs:
DynamoAccessFunction:
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/dynamo_access"
RdsAccessFunction:
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/rds_access"
RdsCreateTableFunction:
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/rds_create_table"