Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ApiGateway Resources #466

Merged
merged 21 commits into from
Apr 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/CONTRIBUTE.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ will be run against any commits to the project.
1. Create a virtualenv (e.g. `virtualenv ~/virtualenv/troposphere`)
1. Activate it: `source ~/virtualenv/troposphere/bin/activate`
1. Install modules and upgrade tools:
1. `pip install --upgrade pip setuptools wheel
1. `pip install --upgrade pip setuptools wheel`
1. `pip install --upgrade pep8 pyflakes`
1. ?? `pip install awacs`
1. Run tests:
Expand All @@ -34,4 +34,4 @@ Tests are run against Python 2.7, 3.3, 3.4, and 3.5.
New example code should go into `troposphere/examples`. The expected
CloudFormation Template should be stored in `troposphere/tests/examples_output/`.
When tests are run the output of the code in the examples directory will
be comprared with the expected results in the example_ouput directory.
be compared with the expected results in the `example_output` directory.
131 changes: 131 additions & 0 deletions examples/ApiGateway.py
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())
175 changes: 175 additions & 0 deletions tests/examples_output/ApiGateway.template
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"
}
}
}
Loading