-
-
Save jgardezi/98c3cb7c96fe5eaf8e74fd6e60309783 to your computer and use it in GitHub Desktop.
This file contains 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: AWS API Gateway with a Lambda Integration | |
Parameters: | |
lambdaFunctionName: | |
Type: "String" | |
AllowedPattern: "^[a-zA-Z0-9]+[a-zA-Z0-9-]+[a-zA-Z0-9]+$" | |
Description: Lambda function name. (Recommend to keep default) | |
Default: "lambda-api" | |
apiStageName: | |
Type: "String" | |
Description: API Staging Name. (Recommend to keep default) | |
Default: "v1" | |
apiResourcePath: | |
Type: "String" | |
Description: Resource Path for API. | |
Default: "hello" | |
Resources: | |
ApiGatewayRestApi: | |
Type: AWS::ApiGateway::RestApi | |
Properties: | |
ApiKeySourceType: HEADER | |
Description: An API Gateway for Lambda APIs | |
EndpointConfiguration: | |
Types: | |
- REGIONAL | |
Name: !Join ["", [{"Ref": "AWS::StackName"}, "-api"]] | |
ProxyResource: | |
Type: 'AWS::ApiGateway::Resource' | |
Properties: | |
RestApiId: !Ref ApiGatewayRestApi | |
ParentId: !GetAtt ApiGatewayRestApi.RootResourceId | |
PathPart: !Ref "apiResourcePath" | |
ProxyResourceANY: | |
Type: 'AWS::ApiGateway::Method' | |
Properties: | |
RestApiId: !Ref ApiGatewayRestApi | |
ResourceId: !Ref ProxyResource | |
HttpMethod: GET | |
ApiKeyRequired: true | |
AuthorizationType: NONE | |
OperationName: !Ref "apiResourcePath" | |
Integration: | |
Type: AWS_PROXY | |
IntegrationHttpMethod: POST | |
Uri: !Sub 'arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunction.Arn}/invocations' | |
ApiGatewayModel: | |
Type: AWS::ApiGateway::Model | |
Properties: | |
ContentType: 'application/json' | |
RestApiId: !Ref ApiGatewayRestApi | |
Schema: {} | |
ApiGatewayStage: | |
Type: AWS::ApiGateway::Stage | |
Properties: | |
DeploymentId: !Ref ApiGatewayDeployment | |
Description: Lambda API Stage v1 | |
RestApiId: !Ref ApiGatewayRestApi | |
StageName: !Ref "apiStageName" | |
ApiGatewayDeployment: | |
Type: AWS::ApiGateway::Deployment | |
DependsOn: ProxyResourceANY | |
Properties: | |
Description: Lambda API Deployment | |
RestApiId: !Ref ApiGatewayRestApi | |
ApiGatewayIamRole: | |
Type: AWS::IAM::Role | |
Properties: | |
AssumeRolePolicyDocument: | |
Version: '2012-10-17' | |
Statement: | |
- Sid: '' | |
Effect: 'Allow' | |
Principal: | |
Service: | |
- 'apigateway.amazonaws.com' | |
Action: | |
- 'sts:AssumeRole' | |
Path: '/' | |
Policies: | |
- PolicyName: LambdaAccess | |
PolicyDocument: | |
Version: '2012-10-17' | |
Statement: | |
- Effect: 'Allow' | |
Action: 'lambda:*' | |
Resource: !GetAtt LambdaFunction.Arn | |
LambdaFunction: | |
Type: AWS::Lambda::Function | |
Properties: | |
Code: | |
ZipFile: | | |
exports.handler = async function(event, context) { | |
return { statusCode: 200, body: 'Hello World!' }; | |
}; | |
Description: API Lambda function | |
FunctionName: !Ref "lambdaFunctionName" | |
Handler: index.handler | |
MemorySize: 512 | |
Role: !GetAtt LambdaIamRole.Arn | |
Runtime: nodejs12.x | |
Timeout: 60 | |
LambdaIamRole: | |
Type: AWS::IAM::Role | |
Properties: | |
AssumeRolePolicyDocument: | |
Version: '2012-10-17' | |
Statement: | |
- Effect: 'Allow' | |
Principal: | |
Service: | |
- 'lambda.amazonaws.com' | |
Action: | |
- 'sts:AssumeRole' | |
Path: '/' | |
Policies: | |
- PolicyDocument: | |
Version: "2012-10-17" | |
Statement: | |
- Action: | |
- "logs:CreateLogGroup" | |
- "logs:CreateLogStream" | |
- "logs:PutLogEvents" | |
Effect: "Allow" | |
Resource: | |
- !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${lambdaFunctionName}:*" | |
PolicyName: !Join ["", [{"Ref": "AWS::StackName"}, "-lambda-log"]] | |
LambdaPermission: | |
Type: AWS::Lambda::Permission | |
Properties: | |
FunctionName: !GetAtt LambdaFunction.Arn | |
Action: lambda:InvokeFunction | |
Principal: 'apigateway.amazonaws.com' | |
LambdaLogGroup: | |
Type: "AWS::Logs::LogGroup" | |
Properties: | |
LogGroupName: !Sub "/aws/lambda/${lambdaFunctionName}" | |
RetentionInDays: 3 | |
ApiKey: | |
Type: AWS::ApiGateway::ApiKey | |
Properties: | |
Name: !Join ["", [{"Ref": "AWS::StackName"}, "-apikey"]] | |
Description: !Join ["", [{"Ref": "AWS::StackName"}, "api key"]] | |
Enabled: true | |
GenerateDistinctId: false | |
ApiUsagePlan: | |
Type: "AWS::ApiGateway::UsagePlan" | |
DependsOn: ApiGatewayStage | |
Properties: | |
ApiStages: | |
- ApiId: !Ref ApiGatewayRestApi | |
Stage: !Ref "apiStageName" | |
Description: !Join [" ", [{"Ref": "AWS::StackName"}, "usage plan"]] | |
UsagePlanName: !Join ["", [{"Ref": "AWS::StackName"}, "-usage-plan"]] | |
ApiUsagePlanKey: | |
Type: "AWS::ApiGateway::UsagePlanKey" | |
Properties: | |
KeyId: !Ref ApiKey | |
KeyType: API_KEY | |
UsagePlanId: !Ref ApiUsagePlan |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment