forked from ikim23/aws-lambda-static-ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverless.yml
168 lines (152 loc) · 4.45 KB
/
serverless.yml
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
service: aws-lambda-static-ip
provider:
name: aws
runtime: nodejs6.10
stage: ${env:STAGE}
region: ${env:REGION}
versionFunctions: false
memorySize: 512
timeout: 10
vpc:
securityGroupIds:
- Fn::GetAtt: [VPCStaticIP, DefaultSecurityGroup]
subnetIds:
- Ref: SubnetPrivate
iamRoleStatements:
- Effect: Allow
Action:
- ec2:CreateNetworkInterface
- ec2:DeleteNetworkInterface
- ec2:DescribeNetworkInterfaces
Resource: '*'
functions:
logger:
handler: functions/logger/logger.handler
events:
- http:
path: log
method: get
integration: lambda
request:
template:
application/json: '{ "sourceIp" : "$context.identity.sourceIp" }'
caller:
environment:
URI:
Fn::Join: ['', [https://, Ref: ApiGatewayRestApi, '.execute-api.${self:provider.region}.amazonaws.com/${self:provider.stage}/log']]
handler: functions/caller/caller.handler
events:
- http:
path: call
method: get
resources:
Resources:
# Resources created according to blog post:
# http://techblog.financialengines.com/2016/09/26/aws-lambdas-with-a-static-outgoing-ip/
# Step 1: Create a new VPC
VPCStaticIP:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 11.0.0.0/16
Tags:
- Key: Name
Value: ${self:service}-${self:provider.stage}-vpc
# Step 2: Create 2 Subnets
SubnetPublic:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ${self:provider.region}b
CidrBlock: 11.0.0.0/24
Tags:
- Key: Name
Value: ${self:service}-${self:provider.stage}-public-subnet
VpcId:
Ref: VPCStaticIP
SubnetPrivate:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ${self:provider.region}b
CidrBlock: 11.0.1.0/24
Tags:
- Key: Name
Value: ${self:service}-${self:provider.stage}-private-subnet
VpcId:
Ref: VPCStaticIP
# Step 3: Create an Internet Gateway
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: ${self:service}-${self:provider.stage}-igw
# Attach Internet Gateway to VPC
VPCGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId:
Ref: InternetGateway
VpcId:
Ref: VPCStaticIP
# Step 4: Create a public Route Table and Assign it to our public route
RouteTablePublic:
Type: AWS::EC2::RouteTable
Properties:
VpcId:
Ref: VPCStaticIP
Tags:
- Key: Name
Value: ${self:service}-${self:provider.stage}-public-route
RoutePublic:
Type: AWS::EC2::Route
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId:
Ref: InternetGateway
RouteTableId:
Ref: RouteTablePublic
SubnetRouteTableAssociationPublic:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId:
Ref: RouteTablePublic
SubnetId:
Ref: SubnetPublic
# Step 5: Create a NAT Gateway
# Before creating NAT Gateway, we need to create Elastic IP with vpc scope
EIP:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
NatGateway:
Type: AWS::EC2::NatGateway
Properties:
AllocationId:
Fn::GetAtt: [EIP, AllocationId]
SubnetId:
Ref: SubnetPublic
# In tutorial NAT Gateway is attached as default route 0.0.0.0/0 in main Route Table.
# Main Route Table is created implicitely during VPC creation and CloudFormation
# has no access to its ID. To overcome this limitation we create additional Route Table.
RouteTablePrivate:
Type: AWS::EC2::RouteTable
Properties:
VpcId:
Ref: VPCStaticIP
Tags:
- Key: Name
Value: ${self:service}-${self:provider.stage}-private-route
RoutePrivate:
Type: AWS::EC2::Route
Properties:
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId:
Ref: NatGateway
RouteTableId:
Ref: RouteTablePrivate
SubnetRouteTableMainAssociationPrivate:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId:
Ref: RouteTablePrivate
SubnetId:
Ref: SubnetPrivate