deployment = t.add_resource( Deployment( "%sDeployment" % stage_name, DependsOn="LambdaMethod", RestApiId=Ref(rest_api), )) stage = t.add_resource( Stage('%sStage' % stage_name, StageName=stage_name, RestApiId=Ref(rest_api), DeploymentId=Ref(deployment))) key = t.add_resource( ApiKey("ApiKey", StageKeys=[StageKey(RestApiId=Ref(rest_api), StageName=Ref(stage))])) # Create an API usage plan usagePlan = t.add_resource( UsagePlan("ExampleUsagePlan", UsagePlanName="ExampleUsagePlan", Description="Example usage plan", Quota=QuotaSettings(Limit=50000, Period="MONTH"), Throttle=ThrottleSettings(BurstLimit=500, RateLimit=5000), ApiStages=[ ApiStage(ApiId=Ref(rest_api), Stage=Ref(stage), Throttle=({ "/some/api/path/GET": ThrottleSettings(BurstLimit=500, RateLimit=5000) }))
RestApiId=Ref(api_gateway), )) stage = template.add_resource(Stage( 'Stage' + stage_name, StageName=stage_name, RestApiId=Ref(api_gateway), DeploymentId=Ref(deployment), )) key = template.add_resource(ApiKey( "ApiKey", Enabled=True, Value=api_key_secret, StageKeys=[StageKey( RestApiId=Ref(api_gateway), StageName=Ref(stage), )], )) usagePlan = template.add_resource(UsagePlan( "UsagePlan", UsagePlanName="UsagePlan", Description="Usage plan for small amount of requests not handles by lambda @ edge.", Quota=QuotaSettings( Limit=1000, Period="MONTH", ), Throttle=ThrottleSettings( RateLimit=50, BurstLimit=200, ),
def add_api_gateway(self, apigateway_name): self.log.info('Adding API Gateway %s' % apigateway_name) assert (self.lambda_function is not None) # define all value used by api gateway lambda_method_name = '%sLambdaMethod' % apigateway_name lambda_permission_name = '%sLambdaPermission' % apigateway_name resource_name = '%sResource' % apigateway_name deployment_name = '%sDeployment' % self.stage_name apikey_name = '%sApiKey' % apigateway_name # start creating api gateway template self.apigateway = RestApi(apigateway_name, Name=apigateway_name) self.template.add_resource(self.apigateway) resource = Resource(resource_name, RestApiId=Ref(self.apigateway), PathPart='{proxy+}', ParentId=GetAtt(apigateway_name, 'RootResourceId')) self.template.add_resource(resource) permission = Permission(lambda_permission_name, Action='lambda:invokeFunction', FunctionName=GetAtt(self.lambda_function, 'Arn'), Principal='apigateway.amazonaws.com', SourceArn=Join("", [ 'arn:aws:execute-api:', Ref('AWS::Region'), ':', Ref('AWS::AccountId'), ':', Ref(self.apigateway), '/*' ])) self.template.add_resource(permission) method = Method( lambda_method_name, DependsOn=lambda_permission_name, RestApiId=Ref(self.apigateway), ResourceId=Ref(resource), HttpMethod='ANY', AuthorizationType='NONE', Integration=Integration( Type='AWS_PROXY', IntegrationHttpMethod='POST', Uri=Join("", [ 'arn:aws:apigateway:', Ref('AWS::Region'), ':lambda:path/2015-03-31/functions/', GetAtt(self.lambda_function, 'Arn'), '/invocations' ])), MethodResponses=[MethodResponse(StatusCode='200')]) self.template.add_resource(method) # create a deployment deployment = Deployment(deployment_name, DependsOn=lambda_method_name, RestApiId=Ref(self.apigateway)) self.template.add_resource(deployment) stage = Stage('%sStage' % self.stage_name, StageName=self.stage_name, RestApiId=Ref(self.apigateway), DeploymentId=Ref(deployment)) self.template.add_resource(stage) key = ApiKey(apikey_name, StageKeys=[ StageKey(RestApiId=Ref(self.apigateway), StageName=Ref(stage)) ]) self.template.add_resource(key)
"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, DependsOn="LambdaMethod", RestApiId=Ref(rest_api), StageName=stage_name)) key = t.add_resource( ApiKey("ApiKey", StageKeys=[StageKey(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"), Output("ApiKey", Value=Ref(key), Description="API key"), ]) print(t.to_json())