Skip to content

Instantly share code, notes, and snippets.

@hassaku63
Last active Oct 4, 2021
Embed
What would you like to do?
serverless.yml example
# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
# docs.serverless.com
#
# Happy Coding!
service: example-service
# app and org for use with dashboard.serverless.com
#app: your-app-name
#org: your-org-name
# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
frameworkVersion: '2'
useDotenv: true
variablesResolutionMode: 20210326
provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: 20201221
stage: ${opt:stage, 'dev'}
region: ${opt:region, 'ap-northeast-1'}
environment:
SLACK_BOT_USERNAME: ${env:SLACK_BOT_USERNAME, 'bot'}
SLACK_BOT_TOKEN: ${env:SLACK_BOT_TOKEN}
SLACK_CHANNEL: ${env:SLACK_CHANNEL}
# you can add statements to the Lambda function's IAM Role here
# iamRoleStatements:
# - Effect: "Allow"
# Action:
# - "s3:ListBucket"
# Resource: { "Fn::Join" : ["", ["arn:aws:s3:::", { "Ref" : "ServerlessDeploymentBucket" } ] ] }
# - Effect: "Allow"
# Action:
# - "s3:PutObject"
# Resource:
# Fn::Join:
# - ""
# - - "arn:aws:s3:::"
# - "Ref" : "ServerlessDeploymentBucket"
# - "/*"
# you can add packaging information here
# package:
# patterns:
# - '!exclude-me.js'
# - '!exclude-me-dir/**'
# - include-me.js
# - include-me-dir/**
custom:
SubscriberLambdaFunctionName: ${self:service}-${self:provider.stage}-notify
functions:
notify:
handler: dist/index.handler
name: ${self:custom.SubscriberLambdaFunctionName}
events:
- httpApi:
path: '/webhook'
method: 'POST'
# The following are a few example events you can configure
# NOTE: Please make sure to change your handler code to work with those events
# Check the event documentation for details
# events:
# - httpApi:
# path: /users/create
# method: get
# - websocket: $connect
# - s3: ${env:BUCKET}
# - schedule: rate(10 minutes)
# - sns: greeter-topic
# - stream: arn:aws:dynamodb:region:XXXXXX:table/foo/stream/1970-01-01T00:00:00.000
# - alexaSkill: amzn1.ask.skill.xx-xx-xx-xx
# - alexaSmartHome: amzn1.ask.skill.xx-xx-xx-xx
# - iot:
# sql: "SELECT * FROM 'some_topic'"
# - cloudwatchEvent:
# event:
# source:
# - "aws.ec2"
# detail-type:
# - "EC2 Instance State-change Notification"
# detail:
# state:
# - pending
# - cloudwatchLog: '/aws/lambda/hello'
# - cognitoUserPool:
# pool: MyUserPool
# trigger: PreSignUp
# - alb:
# listenerArn: arn:aws:elasticloadbalancing:us-east-1:XXXXXX:listener/app/my-load-balancer/50dc6c495c0c9188/
# priority: 1
# conditions:
# host: example.com
# path: /hello
# Define function environment variables here
# environment:
# variable2: value2
# you can add CloudFormation resource templates here
resources:
Resources:
ApplicationDeploymentLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: ${self:service}-${self:provider.stage}-deployment-log-group
RetentionInDays: 30
ApplicationDeploymentLogSubscription:
Type: AWS::Logs::SubscriptionFilter
Properties:
DestinationArn:
arn:aws:lambda:${aws:region}:${aws:accountId}:function:${self:custom.SubscriberLambdaFunctionName}
FilterPattern: "ERROR"
LogGroupName: !Ref ApplicationDeploymentLogGroup
SubscriptionPermission:
Type: AWS::Lambda::Permission
Properties:
Action: "lambda:InvokeFunction"
FunctionName: arn:aws:lambda:${aws:region}:${aws:accountId}:function:${self:custom.SubscriberLambdaFunctionName}
Principal: logs.${aws:region}.amazonaws.com
Outputs:
LogGroupName:
Description: "LogGroup name"
Value: !Ref ApplicationDeploymentLogGroup
LogGroupArn:
Description: "LogGroup ARN"
Value: !GetAtt ApplicationDeploymentLogGroup.Arn
@hassaku63

This comment has been minimized.

Copy link
Owner Author

@hassaku63 hassaku63 commented Sep 27, 2021

README

Serverless Framework のテンプレートに関する、「知っておくと捗る」ちょっとした書き方を紹介する

発表: AWS Dev Day 2021 Lambda だけじゃもったいない。サーバーレス開発の第二歩目を踏み出そう

このテンプレートで作ろうとしている題材について

HTTP API と CloudWatch Logs (subscription filter) からメッセージを受け取り、Slack などの外部システムに通知する Lambda ハンドラの作成、を意図している。

resource desccription
ApplicationDeploymentLogGroup subscribe 対象のロググループ
notify (funcion) ログを受け取って処理する Lambda ハンドラ

環境変数

2021/09/28 時点の最新版の Serverless Framework では、標準機能で dotenv の読み込みに対応している

https://gist.github.com/hassaku63/3447a4381e8001367093ddc61c5ae13f#file-serverless-yml-L31-L34

のような ${env:ENV_VAR} のような記法での参照が可能。

参考: https://www.serverless.com/framework/docs/deprecations/#LOAD_VARIABLES_FROM_ENV_FILES

CloudFormation 疑似パラメータ

2021/09/28 時点の最新版の Serverless Framework では、CloudFormation の疑似パラメータを ${aws:XXX} の記法で参照可能。

CloudFormation 疑似パラメータ参照: https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html

このスニペットでは、

https://gist.github.com/hassaku63/3447a4381e8001367093ddc61c5ae13f#file-serverless-yml-L119-L125

${aws:region}${aws:accountId} が該当する。

${aws:XXX} の変数を使用するためには、yml のトップ階層に variablesResolutionMode を指定する必要がある。

variablesResolutionMode: 20210326

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment