cloudtools / cloudtools/troposphere

How can I integrate SAM with Api Gateway ?

Open
#1,086 0 comments 0 reactions 0 assignees View on GitHub
examples
Dominant language
Python
Stars
4.9k
Forks
1.4k
PR merge metrics
No merged PRs in 30d

Description

What I'm trying to do is use SAM and Api gateway (stage, api key, user plan).
Is is possible for them work together ? How do I reference them to each other ?

```
from troposphere import Template, Ref
from troposphere.awslambda import Environment
from troposphere.serverless import Function, ApiEvent, SimpleTable, Api
from troposphere.apigateway import RestApi, Method
from troposphere.apigateway import Deployment, Stage, ApiStage

t = Template()

t.add_description(
"Simple CRUD webservice. State is stored in a SimpleTable (DynamoDB) "
"resource.")

t.add_transform('AWS::Serverless-2016-10-31')

# Api Gateway
rest_api = t.add_resource(RestApi(
"ExampleApi",
Name="ExampleApi"
))

# Create a deployment
stage_name = 'dev'

deployment = t.add_resource(Deployment(
f"Deployment{stage_name}",
DependsOn="OmmaClaimLambda", # this is not working
RestApiId=Ref(rest_api),
))

stage = t.add_resource(Stage(
f"Stage{stage_name}",
StageName=stage_name,
RestApiId=Ref(rest_api),
DeploymentId=Ref(deployment)
))

api = t.add_resource(
Api(
"api",
StageName="dev",
DefinitionBody="",
)
)

# SAM

t.add_resource(
Function(
"OmmaClaimLambda",
Handler='index.handler',
Runtime='nodejs8.10',
CodeUri='s3://sam-demo-zd/lambda.zip',
Environment=Environment(
Variables={
}
),
Events={
'GenReport': ApiEvent(
'GetResource',
Path='/report',
Method='post',
),
'PostLineNum': ApiEvent(
'GetResource',
Path='/linenum',
Method='post'
),
}
)
)

print(t.to_json())

# Finally, write the template to a file
with open('LambdaMultipleApi.json', 'w') as f:
f.write(t.to_json())

# Finally, write the template to a file
with open('LambdaMultipleApi.yaml', 'w') as f:
f.write(t.to_yaml())

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.