Created
December 10, 2016 02:20
-
-
Save moduspwnens/3742770fd3391ef9f03ad344ae0871b8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
--- | |
AWSTemplateFormatVersion: '2010-09-09' | |
Description: An example API with a single resource and Lambda-backed method. | |
Mappings: | |
StaticVariables: | |
Main: | |
DummyStageName: DummyStage | |
ExampleApiHttpMethod: GET | |
StageName: v1 | |
Resources: | |
ApiGatewayCloudWatchRole: | |
Type: AWS::IAM::Role | |
Properties: | |
AssumeRolePolicyDocument: | |
Version: '2012-10-17' | |
Statement: | |
- Effect: Allow | |
Principal: | |
Service: | |
- apigateway.amazonaws.com | |
Action: | |
- sts:AssumeRole | |
Path: "/" | |
ManagedPolicyArns: | |
- arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs | |
DependsOn: | |
- ExampleApiDummyStageLogGroup | |
- ExampleApiMainStageLogGroup | |
ApiGatewayAccount: | |
Type: AWS::ApiGateway::Account | |
Properties: | |
CloudWatchRoleArn: | |
Fn::GetAtt: | |
- ApiGatewayCloudWatchRole | |
- Arn | |
ExampleApi: | |
Type: AWS::ApiGateway::RestApi | |
Properties: | |
Name: | |
Ref: AWS::StackName | |
Description: An example REST API | |
ExampleApiDeployment: | |
Type: AWS::ApiGateway::Deployment | |
Properties: | |
RestApiId: | |
Ref: ExampleApi | |
StageName: | |
Fn::FindInMap: | |
- StaticVariables | |
- Main | |
- DummyStageName | |
DependsOn: | |
- ExampleApiDummyStageLogGroup | |
- ExampleApiMethod | |
ExampleApiDummyStageLogGroup: | |
Type: AWS::Logs::LogGroup | |
Properties: | |
LogGroupName: | |
Fn::Sub: | |
- API-Gateway-Execution-Logs_${ExampleApi}/${DummyStageName} | |
- DummyStageName: | |
Fn::FindInMap: | |
- StaticVariables | |
- Main | |
- DummyStageName | |
ExampleApiStage: | |
Type: AWS::ApiGateway::Stage | |
Properties: | |
DeploymentId: | |
Ref: ExampleApiDeployment | |
MethodSettings: | |
- ResourcePath: '' | |
HttpMethod: "*/*" | |
DataTraceEnabled: true | |
LoggingLevel: INFO | |
MetricsEnabled: false | |
RestApiId: | |
Ref: ExampleApi | |
StageName: | |
Fn::FindInMap: | |
- StaticVariables | |
- Main | |
- StageName | |
DependsOn: | |
- ExampleApiMainStageLogGroup | |
ExampleApiMainStageLogGroup: | |
Type: AWS::Logs::LogGroup | |
Properties: | |
LogGroupName: | |
Fn::Sub: | |
- API-Gateway-Execution-Logs_${ExampleApi}/${MainStageName} | |
- MainStageName: | |
Fn::FindInMap: | |
- StaticVariables | |
- Main | |
- StageName | |
ExampleApiExampleResource: | |
Type: AWS::ApiGateway::Resource | |
Properties: | |
ParentId: | |
Fn::GetAtt: | |
- ExampleApi | |
- RootResourceId | |
PathPart: example | |
RestApiId: | |
Ref: ExampleApi | |
# | |
# Example Function | |
# | |
ExampleFunction: | |
Type: AWS::Lambda::Function | |
Properties: | |
Description: An example function to respond to API Gateway requests. | |
Handler: index.lambda_handler | |
MemorySize: 128 | |
Role: | |
Fn::GetAtt: | |
- ExampleFunctionRole | |
- Arn | |
Code: | |
ZipFile: |- | |
import json | |
def lambda_handler(event, context): | |
print(json.dumps(event)) | |
response_body = { | |
"message": "Hello World!" | |
} | |
return { | |
"statusCode": 200, | |
"headers": { | |
"Content-Type": "application/json" | |
}, | |
"body": json.dumps(response_body) | |
} | |
Runtime: python2.7 | |
Timeout: '300' | |
ExampleFunctionRole: | |
Type: AWS::IAM::Role | |
Properties: | |
AssumeRolePolicyDocument: | |
Version: '2012-10-17' | |
Statement: | |
- Effect: Allow | |
Principal: | |
Service: | |
- lambda.amazonaws.com | |
Action: | |
- sts:AssumeRole | |
Path: "/" | |
ExampleFunctionRoleActions: | |
Type: AWS::IAM::Policy | |
Properties: | |
PolicyName: ExampleFunctionRoleActions | |
PolicyDocument: | |
Version: '2012-10-17' | |
Statement: | |
- Effect: Allow | |
Action: | |
- logs:CreateLogStream | |
- logs:PutLogEvents | |
Resource: | |
Fn::Sub: arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${ExampleFunction}:log-stream:* | |
Roles: | |
- Ref: ExampleFunctionRole | |
ExampleFunctionLogGroup: | |
Type: AWS::Logs::LogGroup | |
Properties: | |
LogGroupName: | |
Fn::Sub: /aws/lambda/${ExampleFunction} | |
ExampleApiMethod: | |
Type: AWS::ApiGateway::Method | |
Properties: | |
AuthorizationType: NONE | |
ResourceId: | |
Ref: ExampleApiExampleResource | |
RestApiId: | |
Ref: ExampleApi | |
HttpMethod: | |
Fn::FindInMap: | |
- StaticVariables | |
- Main | |
- ExampleApiHttpMethod | |
Integration: | |
IntegrationHttpMethod: POST | |
IntegrationResponses: | |
- StatusCode: '200' | |
SelectionPattern: '' | |
PassthroughBehavior: WHEN_NO_MATCH | |
Type: AWS_PROXY | |
Uri: | |
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ExampleFunction.Arn}/invocations | |
MethodResponses: | |
- StatusCode: '200' | |
DependsOn: | |
- ExampleApiMethodExampleLambdaPermission | |
ExampleApiMethodExampleLambdaPermission: | |
Type: AWS::Lambda::Permission | |
Properties: | |
Action: lambda:InvokeFunction | |
FunctionName: | |
Ref: ExampleFunction | |
Principal: apigateway.amazonaws.com | |
SourceArn: | |
Fn::Sub: | |
- arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${ExampleApi}/*/${ExampleApiHttpMethod}/example | |
- ExampleApiHttpMethod: | |
Fn::FindInMap: | |
- StaticVariables | |
- Main | |
- ExampleApiHttpMethod | |
DependsOn: | |
- ExampleFunctionRoleActions | |
- ExampleFunctionLogGroup | |
Outputs: | |
ExampleApiEndpoint: | |
Value: | |
Fn::Sub: https://${ExampleApi}.execute-api.${AWS::Region}.amazonaws.com/${ExampleApiStage}/example/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment