troposphere icon indicating copy to clipboard operation
troposphere copied to clipboard

How can I integrate SAM with Api Gateway ?

Open bluedusk opened this issue 7 years ago • 0 comments

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())

bluedusk avatar Jun 27 '18 06:06 bluedusk