-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added the Api Gateway Resources - Added an example integration for the Api Gateway, adding a Lambda function, Resource and Method Integration
- Loading branch information
Showing
4 changed files
with
537 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
from troposphere import Ref, Template, Output | ||
from troposphere.apigateway import RestApi, Method | ||
from troposphere.apigateway import Resource, MethodResponse | ||
from troposphere.apigateway import Integration, IntegrationResponse | ||
from troposphere.apigateway import Deployment | ||
from troposphere.iam import Role, Policy | ||
from troposphere.awslambda import Function, Code | ||
from troposphere import GetAtt, Join | ||
|
||
|
||
t = Template() | ||
|
||
# Create the Api Gateway | ||
rest_api = t.add_resource(RestApi( | ||
"ExampleApi", | ||
Name="ExampleApi" | ||
)) | ||
|
||
# Create a Lambda function that will be mapped | ||
code = [ | ||
"var response = require('cfn-response');", | ||
"exports.handler = function(event, context) {", | ||
" context.succeed('foobar!');", | ||
" return 'foobar!';", | ||
"};", | ||
] | ||
|
||
# Create a role for the lambda function | ||
t.add_resource(Role( | ||
"LambdaExecutionRole", | ||
Path="/", | ||
Policies=[Policy( | ||
PolicyName="root", | ||
PolicyDocument={ | ||
"Version": "2012-10-17", | ||
"Statement": [{ | ||
"Action": ["logs:*"], | ||
"Resource": "arn:aws:logs:*:*:*", | ||
"Effect": "Allow" | ||
}, { | ||
"Action": ["lambda:*"], | ||
"Resource": "*", | ||
"Effect": "Allow" | ||
}] | ||
})], | ||
AssumeRolePolicyDocument={"Version": "2012-10-17", "Statement": [ | ||
{ | ||
"Action": ["sts:AssumeRole"], | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": [ | ||
"lambda.amazonaws.com", | ||
"apigateway.amazonaws.com" | ||
] | ||
} | ||
} | ||
]}, | ||
)) | ||
|
||
# Create the Lambda function | ||
foobar_function = t.add_resource(Function( | ||
"FoobarFunction", | ||
Code=Code( | ||
ZipFile=Join("", code) | ||
), | ||
Handler="index.handler", | ||
Role=GetAtt("LambdaExecutionRole", "Arn"), | ||
Runtime="nodejs", | ||
)) | ||
|
||
# Create a resource to map the lambda function to | ||
resource = t.add_resource(Resource( | ||
"FoobarResource", | ||
RestApiId=Ref(rest_api), | ||
PathPart="foobar", | ||
ParentId=GetAtt("ExampleApi", "RootResourceId"), | ||
)) | ||
|
||
# Create a Lambda API method for the Lambda resource | ||
method = t.add_resource(Method( | ||
"LambdaMethod", | ||
DependsOn='FoobarFunction', | ||
RestApiId=Ref(rest_api), | ||
AuthorizationType="NONE", | ||
ResourceId=Ref(resource), | ||
HttpMethod="GET", | ||
Integration=Integration( | ||
Credentials=GetAtt("LambdaExecutionRole", "Arn"), | ||
Type="AWS", | ||
IntegrationHttpMethod='POST', | ||
IntegrationResponses=[ | ||
IntegrationResponse( | ||
StatusCode='200' | ||
) | ||
], | ||
Uri=Join("", [ | ||
"arn:aws:apigateway:eu-west-1:lambda:path/2015-03-31/functions/", | ||
GetAtt("FoobarFunction", "Arn"), | ||
"/invocations" | ||
]) | ||
), | ||
MethodResponses=[ | ||
MethodResponse( | ||
"CatResponse", | ||
StatusCode='200' | ||
) | ||
] | ||
)) | ||
|
||
# Create a deployment | ||
stage_name = 'v1' | ||
deployment = t.add_resource(Deployment( | ||
"%sDeployment" % stage_name, | ||
RestApiId=Ref(rest_api), | ||
StageName=stage_name | ||
)) | ||
|
||
# Add the deployment endpoint as an output | ||
t.add_output(Output( | ||
"ApiEndpoint", | ||
Value=Join("", [ | ||
"https://", | ||
Ref(rest_api), | ||
".execute-api.eu-west-1.amazonaws.com/", | ||
stage_name | ||
]), | ||
Description="Endpoint for this stage of the api" | ||
)) | ||
|
||
|
||
print(t.to_json()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
{ | ||
"Outputs": { | ||
"ApiEndpoint": { | ||
"Description": "Endpoint for this stage of the api", | ||
"Value": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"https://", | ||
{ | ||
"Ref": "ExampleApi" | ||
}, | ||
".execute-api.eu-west-1.amazonaws.com/", | ||
"v1" | ||
] | ||
] | ||
} | ||
} | ||
}, | ||
"Resources": { | ||
"ExampleApi": { | ||
"Properties": { | ||
"Name": "ExampleApi" | ||
}, | ||
"Type": "AWS::ApiGateway::RestApi" | ||
}, | ||
"FoobarFunction": { | ||
"Properties": { | ||
"Code": { | ||
"ZipFile": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"var response = require('cfn-response');", | ||
"exports.handler = function(event, context) {", | ||
" context.succeed('foobar!');", | ||
" return 'foobar!';", | ||
"};" | ||
] | ||
] | ||
} | ||
}, | ||
"Handler": "index.handler", | ||
"Role": { | ||
"Fn::GetAtt": [ | ||
"LambdaExecutionRole", | ||
"Arn" | ||
] | ||
}, | ||
"Runtime": "nodejs" | ||
}, | ||
"Type": "AWS::Lambda::Function" | ||
}, | ||
"FoobarResource": { | ||
"Properties": { | ||
"ParentId": { | ||
"Fn::GetAtt": [ | ||
"ExampleApi", | ||
"RootResourceId" | ||
] | ||
}, | ||
"PathPart": "foobar", | ||
"RestApiId": { | ||
"Ref": "ExampleApi" | ||
} | ||
}, | ||
"Type": "AWS::ApiGateway::Resource" | ||
}, | ||
"LambdaExecutionRole": { | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"sts:AssumeRole" | ||
], | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": [ | ||
"lambda.amazonaws.com", | ||
"apigateway.amazonaws.com" | ||
] | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"Path": "/", | ||
"Policies": [ | ||
{ | ||
"PolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"logs:*" | ||
], | ||
"Effect": "Allow", | ||
"Resource": "arn:aws:logs:*:*:*" | ||
}, | ||
{ | ||
"Action": [ | ||
"lambda:*" | ||
], | ||
"Effect": "Allow", | ||
"Resource": "*" | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"PolicyName": "root" | ||
} | ||
] | ||
}, | ||
"Type": "AWS::IAM::Role" | ||
}, | ||
"LambdaMethod": { | ||
"DependsOn": "FoobarFunction", | ||
"Properties": { | ||
"AuthorizationType": "NONE", | ||
"HttpMethod": "GET", | ||
"Integration": { | ||
"Credentials": { | ||
"Fn::GetAtt": [ | ||
"LambdaExecutionRole", | ||
"Arn" | ||
] | ||
}, | ||
"IntegrationHttpMethod": "POST", | ||
"IntegrationResponses": [ | ||
{ | ||
"StatusCode": "200" | ||
} | ||
], | ||
"Type": "AWS", | ||
"Uri": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"arn:aws:apigateway:eu-west-1:lambda:path/2015-03-31/functions/", | ||
{ | ||
"Fn::GetAtt": [ | ||
"FoobarFunction", | ||
"Arn" | ||
] | ||
}, | ||
"/invocations" | ||
] | ||
] | ||
} | ||
}, | ||
"MethodResponses": [ | ||
{ | ||
"StatusCode": "200" | ||
} | ||
], | ||
"ResourceId": { | ||
"Ref": "FoobarResource" | ||
}, | ||
"RestApiId": { | ||
"Ref": "ExampleApi" | ||
} | ||
}, | ||
"Type": "AWS::ApiGateway::Method" | ||
}, | ||
"v1Deployment": { | ||
"Properties": { | ||
"RestApiId": { | ||
"Ref": "ExampleApi" | ||
}, | ||
"StageName": "v1" | ||
}, | ||
"Type": "AWS::ApiGateway::Deployment" | ||
} | ||
} | ||
} |
Oops, something went wrong.