Skip to content

Instantly share code, notes, and snippets.

@allanchua101
Created August 22, 2020 15:05
Show Gist options
  • Save allanchua101/5f6dcbd974f91a911933e75e8f418f4f to your computer and use it in GitHub Desktop.
Save allanchua101/5f6dcbd974f91a911933e75e8f418f4f to your computer and use it in GitHub Desktop.
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >-
SAM template for provisioning the following resources:
- Lambda APIs
- Lambda Layer
- API Gateway
# ___ _
# / _ \__ _ _ __ __ _ _ __ ___ ___| |_ ___ _ __ ___
# / /_)/ _` | '__/ _` | '_ ` _ \ / _ \ __/ _ \ '__/ __|
# / ___/ (_| | | | (_| | | | | | | __/ || __/ | \__ \
# \/ \__,_|_| \__,_|_| |_| |_|\___|\__\___|_| |___/
#
Parameters:
Environment:
Type: String
Description: "Environment code of deployment (dev, uat, prod)"
AllowedPattern: ".+"
AllowedValues:
- "dev"
- "uat"
- "prod"
AppName:
Type: String
Description: "Name of application"
Default: "ninja-slack-chatops"
# ____
# | _ \ ___ ___ ___ _ _ _ __ ___ ___ ___
# | |_) / _ \/ __|/ _ \| | | | '__/ __/ _ \/ __|
# | _ < __/\__ \ (_) | |_| | | | (_| __/\__ \
# |_| \_\___||___/\___/ \__,_|_| \___\___||___/
#
Resources:
# ___ _
# / _ \__ _| |_ _____ ____ _ _ _
# / /_\/ _` | __/ _ \ \ /\ / / _` | | | |
# / /_\\ (_| | || __/\ V V / (_| | |_| |
# \____/\__,_|\__\___| \_/\_/ \__,_|\__, |
# |___/
NinjaLayersApiGateway:
Type: "AWS::Serverless::Api"
Properties:
StageName: "v1"
Cors: "'*'"
Auth:
ApiKeyRequired: true
GatewayResponses:
UNAUTHORIZED:
StatusCode: 401
ResponseParameters:
Headers:
Access-Control-Expose-Headers: "'WWW-Authenticate'"
Access-Control-Allow-Origin: "'*'"
WWW-Authenticate: >-
'Bearer realm="admin"'
NinjaLayersApiKey:
Type: AWS::ApiGateway::ApiKey
DependsOn: NinjaLayersApiGateway
Properties:
Name: !Join ["-", [!Ref AWS::StackName, "-api-key"]]
Description: "API key used for invoking layers api gateway"
Enabled: true
GenerateDistinctId: false
StageKeys:
- RestApiId: !Ref NinjaLayersApiGateway
StageName: !Ref NinjaLayersApiGateway.Stage
NinjaLayersUsagePlan:
Type: AWS::ApiGateway::UsagePlan
DependsOn: NinjaLayersApiGateway
Properties:
ApiStages:
- ApiId: !Ref NinjaLayersApiGateway
Stage: !Ref NinjaLayersApiGateway.Stage
Description: !Join [" ", [!Ref AWS::StackName, "usage plan"]]
Quota:
Limit: 10000
Period: MONTH
Throttle:
BurstLimit: 1000
RateLimit: 1000
UsagePlanName: !Join ["-", [!Ref AWS::StackName, "usage-plan"]]
NinjaLayersApiUsagePlanKey:
Type: AWS::ApiGateway::UsagePlanKey
DependsOn: NinjaLayersUsagePlan
Properties:
KeyId: !Ref NinjaLayersApiKey
KeyType: API_KEY
UsagePlanId: !Ref NinjaLayersUsagePlan
# _ ___ _____
# /_\ / _ \\_ \___
# //_\\ / /_)/ / /\/ __|
# / _ \/ ___/\/ /_ \__ \
# \_/ \_/\/ \____/ |___/
ApiGetWeapons:
Type: "AWS::Serverless::Function"
Properties:
FunctionName: !Join ["-", [!Ref Environment, !Ref AppName, "get-weapons"]]
CodeUri: weapons-get-list/
Handler: app.execute
Timeout: 30 # Seconds
Runtime: nodejs10.x
MemorySize: 512
Layers:
- !Ref NodeBaseLayer
Policies:
- DynamoDBCrudPolicy:
TableName: !Join ["-", [!Ref Environment, !Ref AppName, "weapons"]]
Environment:
Variables:
API_NAME: "REST API - Get Weapons"
ENVIRONMENT: !Ref Environment
AWS: True
WEAPONS_TABLE:
!Join ["-", [!Ref Environment, !Ref AppName, "weapons"]]
Events:
GetResource:
Type: "Api"
Properties:
RestApiId: !Ref NinjaLayersApiGateway
Path: "/ninja/weapons"
Method: "GET"
Auth:
ApiKeyRequired: true
Options:
Type: "Api"
Properties:
RestApiId: !Ref NinjaLayersApiGateway
Path: "/ninja/weapons"
Method: OPTIONS
Auth:
ApiKeyRequired: false
Tags:
APP_NAME: !Ref AppName
ENVIRONMENT: !Ref Environment
ApiGetNinjas:
Type: "AWS::Serverless::Function"
Properties:
FunctionName: !Join ["-", [!Ref Environment, !Ref AppName, "get-ninjas"]]
CodeUri: ninja-get-list/
Handler: app.execute
Timeout: 30 # Seconds
Runtime: nodejs10.x
MemorySize: 512
Layers:
- !Ref NodeBaseLayer
Policies:
- DynamoDBCrudPolicy:
TableName: !Join ["-", [!Ref Environment, !Ref AppName, "ninjas"]]
Environment:
Variables:
API_NAME: "REST API - Get Reds"
ENVIRONMENT: !Ref Environment
AWS: True
NINJAS_TABLE: !Join ["-", [!Ref Environment, !Ref AppName, "ninjas"]]
Events:
GetResource:
Type: "Api"
Properties:
RestApiId: !Ref NinjaLayersApiGateway
Path: "/ninjas"
Method: "GET"
Auth:
ApiKeyRequired: true
Options:
Type: "Api"
Properties:
RestApiId: !Ref NinjaLayersApiGateway
Path: "/ninjas"
Method: OPTIONS
Auth:
ApiKeyRequired: false
Tags:
APP_NAME: !Ref AppName
ENVIRONMENT: !Ref Environment
NodeBaseLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName:
!Join ["-", [!Ref Environment, !Ref AppName, "base-layer-node"]]
Description: Dependencies for Ninja APP.
ContentUri: base-layer/
CompatibleRuntimes:
- nodejs10.x
LicenseInfo: "MIT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment