Skip to content

Commit 55dba6a

Browse files
Updated template.yaml with SAM template
1 parent 5503bc0 commit 55dba6a

File tree

1 file changed

+106
-125
lines changed

1 file changed

+106
-125
lines changed
Lines changed: 106 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
AWSTemplateFormatVersion: 2010-09-09
2+
Transform: AWS::Serverless-2016-10-31
23
Description: Amazon API Gateway REST API with VPC Link integration with NLB -> ALB Integration
4+
35
Parameters:
46
VPCCIDR:
57
Description: Enter CIDR for new VPC
@@ -28,7 +30,7 @@ Resources:
2830
EnableDnsHostnames: true
2931
CidrBlock: !Ref VPCCIDR
3032
InstanceTenancy: "default"
31-
33+
3234
PrivateSubnet1:
3335
Type: "AWS::EC2::Subnet"
3436
Properties:
@@ -52,99 +54,90 @@ Resources:
5254
-
5355
Key: "Name"
5456
Value: "Private-new-availability-2"
55-
57+
5658
EC2SecurityGroup:
57-
Type: "AWS::EC2::SecurityGroup"
58-
Properties:
59-
GroupDescription: "Allow VPC CIDR"
60-
GroupName: "PrivateLoadBalancerSG"
61-
VpcId: !Ref EC2VPC
62-
SecurityGroupIngress:
63-
-
64-
CidrIp: !Ref VPCCIDR
65-
FromPort: 443
66-
IpProtocol: "tcp"
67-
ToPort: 443
68-
-
69-
CidrIp: !Ref VPCCIDR
70-
FromPort: 80
71-
IpProtocol: "tcp"
72-
ToPort: 80
73-
SecurityGroupEgress:
74-
-
75-
CidrIp: !Ref VPCCIDR
76-
FromPort: 443
77-
IpProtocol: "tcp"
78-
ToPort: 443
79-
-
80-
CidrIp: !Ref VPCCIDR
81-
FromPort: 80
82-
IpProtocol: "tcp"
83-
ToPort: 80
84-
85-
IAMRole:
86-
Type: "AWS::IAM::Role"
87-
Properties:
88-
Path: "/"
89-
RoleName: "LambdaRole"
90-
AssumeRolePolicyDocument: "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Action\":\"sts:AssumeRole\"}]}"
91-
MaxSessionDuration: 3600
92-
ManagedPolicyArns:
93-
- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
94-
Description: "Allows Lambda functions to call AWS services on your behalf."
95-
59+
Type: "AWS::EC2::SecurityGroup"
60+
Properties:
61+
GroupDescription: "Allow VPC CIDR"
62+
GroupName: "PrivateLoadBalancerSG"
63+
VpcId: !Ref EC2VPC
64+
SecurityGroupIngress:
65+
-
66+
CidrIp: !Ref VPCCIDR
67+
FromPort: 443
68+
IpProtocol: "tcp"
69+
ToPort: 443
70+
-
71+
CidrIp: !Ref VPCCIDR
72+
FromPort: 80
73+
IpProtocol: "tcp"
74+
ToPort: 80
75+
SecurityGroupEgress:
76+
-
77+
CidrIp: !Ref VPCCIDR
78+
FromPort: 443
79+
IpProtocol: "tcp"
80+
ToPort: 443
81+
-
82+
CidrIp: !Ref VPCCIDR
83+
FromPort: 80
84+
IpProtocol: "tcp"
85+
ToPort: 80
86+
87+
# Using SAM's simplified Lambda function definition
9688
LambdaFunction:
97-
Type: "AWS::Lambda::Function"
98-
Properties:
99-
Description: "AWS Lambda target for ALB"
100-
FunctionName: "ALBTargetLambda"
101-
Handler: "index.lambda_handler"
102-
Architectures:
103-
- "x86_64"
104-
Code:
105-
ZipFile: |
106-
import json
107-
108-
def lambda_handler(event, context):
109-
return {
110-
'statusCode': 200,
111-
'body': json.dumps('Hello from Lambda behind NLB -> ALB Integration!')
112-
}
113-
MemorySize: 128
114-
Role: !GetAtt IAMRole.Arn
115-
Runtime: "python3.11"
116-
Timeout: 15
117-
TracingConfig:
118-
Mode: "PassThrough"
119-
EphemeralStorage:
120-
Size: 512
121-
89+
Type: "AWS::Serverless::Function"
90+
Properties:
91+
Description: "AWS Lambda target for ALB"
92+
FunctionName: "ALBTargetLambda"
93+
Handler: "index.lambda_handler"
94+
Runtime: "python3.11"
95+
Architectures:
96+
- "x86_64"
97+
MemorySize: 128
98+
Timeout: 15
99+
EphemeralStorage:
100+
Size: 512
101+
InlineCode: |
102+
import json
103+
104+
def lambda_handler(event, context):
105+
return {
106+
'statusCode': 200,
107+
'body': json.dumps('Hello from Lambda behind NLB -> ALB Integration!')
108+
}
109+
Tracing: PassThrough
110+
# SAM automatically creates the execution role with basic permissions
111+
# but we can specify a managed policy to match the original template
112+
Policies:
113+
- AWSLambdaBasicExecutionRole
114+
122115
LambdaALBPermission:
123116
Type: AWS::Lambda::Permission
124117
Properties:
125118
FunctionName: !GetAtt LambdaFunction.Arn
126119
Action: lambda:InvokeFunction
127120
Principal: elasticloadbalancing.amazonaws.com
128-
121+
129122
PrivateALB:
130123
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
131124
Properties:
132125
Type: application
133126
Scheme: internal
134127
Name: PrivateALB
135128
Subnets:
136-
- !Ref PrivateSubnet1
137-
- !Ref PrivateSubnet2
129+
- !Ref PrivateSubnet1
130+
- !Ref PrivateSubnet2
138131
SecurityGroups: [!Ref EC2SecurityGroup]
139-
132+
140133
ALBTargetGroup:
141134
Type: AWS::ElasticLoadBalancingV2::TargetGroup
142135
DependsOn: LambdaALBPermission
143136
Properties:
144137
TargetType: lambda
145138
Targets:
146139
- Id: !GetAtt LambdaFunction.Arn
147-
140+
148141
ALBHttpListener:
149142
Type: AWS::ElasticLoadBalancingV2::Listener
150143
Properties:
@@ -156,17 +149,17 @@ Resources:
156149
DefaultActions:
157150
- TargetGroupArn: !Ref ALBTargetGroup
158151
Type: forward
159-
152+
160153
PrivateNLB:
161154
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
162155
Properties:
163156
Type: network
164157
Scheme: internal
165158
Name: PrivateNLB
166159
Subnets:
167-
- !Ref PrivateSubnet1
168-
- !Ref PrivateSubnet2
169-
160+
- !Ref PrivateSubnet1
161+
- !Ref PrivateSubnet2
162+
170163
NLBTargetGroup:
171164
Type: AWS::ElasticLoadBalancingV2::TargetGroup
172165
Properties:
@@ -187,59 +180,47 @@ Resources:
187180
- TargetGroupArn: !Ref NLBTargetGroup
188181
Type: forward
189182

190-
# REST API Part
183+
# Using SAM's simplified API Gateway definition
191184
PrivateIntApi:
192-
Type: AWS::ApiGateway::RestApi
185+
Type: AWS::Serverless::Api
193186
Properties:
194187
Name: apigw-with-alb
195-
Description: VPC Link integration REST API with NLB ALB as backend
196-
197-
RootMethodGet:
198-
Type: AWS::ApiGateway::Method
199-
Properties:
200-
RestApiId: !Ref PrivateIntApi
201-
ResourceId: !GetAtt PrivateIntApi.RootResourceId
202-
HttpMethod: GET
203-
AuthorizationType: NONE
204-
Integration:
205-
Type: HTTP
206-
ConnectionType: VPC_LINK
207-
ConnectionId: !Ref VPCLinkRest
208-
IntegrationHttpMethod: ANY
209-
Uri: !Sub "https://${AlbInternalCertificateDns}"
210-
PassthroughBehavior: WHEN_NO_MATCH
211-
TimeoutInMillis: 29000
212-
IntegrationResponses:
213-
- StatusCode: 200
214-
ResponseParameters:
215-
method.response.header.Access-Control-Allow-Origin: "'*'"
216-
method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
217-
method.response.header.Access-Control-Allow-Methods: "'GET'"
218-
MethodResponses:
219-
- StatusCode: 200
220-
ResponseParameters:
221-
method.response.header.Access-Control-Allow-Origin: true
222-
method.response.header.Access-Control-Allow-Headers: true
223-
method.response.header.Access-Control-Allow-Methods: true
224-
ResponseModels:
225-
application/json: 'Empty'
226-
OperationName: 'RootOperation'
227-
228-
229-
230-
Deployment:
231-
Type: AWS::ApiGateway::Deployment
232-
DependsOn:
233-
- RootMethodGet
234-
Properties:
235-
RestApiId: !Ref PrivateIntApi
236-
237-
Stage:
238-
Type: AWS::ApiGateway::Stage
239-
Properties:
240188
StageName: Prod
241-
RestApiId: !Ref PrivateIntApi
242-
DeploymentId: !Ref Deployment
189+
EndpointConfiguration: REGIONAL
190+
DefinitionBody:
191+
swagger: "2.0"
192+
info:
193+
title: "apigw-with-alb"
194+
description: "VPC Link integration REST API with NLB ALB as backend"
195+
paths:
196+
/:
197+
get:
198+
x-amazon-apigateway-integration:
199+
type: http
200+
connectionType: VPC_LINK
201+
connectionId: !Ref VPCLinkRest
202+
httpMethod: ANY
203+
uri: !Sub "https://${AlbInternalCertificateDns}"
204+
passthroughBehavior: when_no_match
205+
timeoutInMillis: 29000
206+
responses:
207+
default:
208+
statusCode: "200"
209+
responseParameters:
210+
method.response.header.Access-Control-Allow-Origin: "'*'"
211+
method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
212+
method.response.header.Access-Control-Allow-Methods: "'GET'"
213+
responses:
214+
"200":
215+
description: "200 response"
216+
headers:
217+
Access-Control-Allow-Origin:
218+
type: "string"
219+
Access-Control-Allow-Headers:
220+
type: "string"
221+
Access-Control-Allow-Methods:
222+
type: "string"
223+
operationId: "RootOperation"
243224

244225
VPCLinkRest:
245226
Type: AWS::ApiGateway::VpcLink
@@ -251,4 +232,4 @@ Resources:
251232
Outputs:
252233
PrivateIntApiEndpoint:
253234
Description: API Endpoint
254-
Value: !Sub "https://${PrivateIntApi}.execute-api.${AWS::Region}.amazonaws.com/Prod"
235+
Value: !Sub "https://${PrivateIntApi}.execute-api.${AWS::Region}.amazonaws.com/Prod"

0 commit comments

Comments
 (0)