- 公開日
- 最終更新日
【SAM】AWSリソースの共通設定を集中管理する
この記事を共有する
はじめに
パーソル&サーバーワークス嶋田です。
SAMを使ってAWSリソースを構築しているときに、
複数のリソースに跨って同じ値を設定することはよくあることかと思います。
これらの値を、全てのリソースで定義するのは少し面倒です。
もしかすると、SAMテンプレートの Globals が解決してくれるかもしれないです。
Globals とは
SAMテンプレートで記述できる設定値で、
テンプレート内で定義する特定のリソース設定値を、
グローバルに定義できる領域です。
Globals を活用することで、同じ設定を繰り返し記述せずに済みます。
注意点としては、全てのSAMリソースに有効な設定値ではない です。
Globalsを採用するかどうかはドキュメントを確認の上、判断ください。
2025年3月9日時点では、以下のリソースが対象とのことです。
- AWS::Serverless::Api
- AWS::Serverless::Function
- AWS::Serverless::HttpApi
- AWS::Serverless::SimpleTable
- AWS::Serverless::StateMachine
具体例
以下は、 sam init で作成したSAMテンプレートです。
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
sam-app
Sample SAM Template for sam-app
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs22.x
Architectures:
- x86_64
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /hello
Method: get
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt HelloWorldFunctionRole.Arn
今回の主役は、 Globals セクションです。
現状、LambdaのTimeout値だけ設定されています。
Globalsの挙動を確かめる
テンプレートに記述されている HelloWorldFunction を複製して、Timeoutの値を30に変更します。
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
sam-app
Sample SAM Template for sam-app
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 30
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs22.x
Architectures:
- x86_64
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /hello
Method: get
HelloWorldFunction2:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs22.x
Architectures:
- x86_64
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /hello
Method: get
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt HelloWorldFunctionRole.Arn
テンプレートを書き換えたら、sam build && sam deploy --no-confirm-changeset を実行します。
Successfully created/updated stack - sam-app in ap-northeast-1
と表示されたら、デプロイ成功です。
デプロイしたLambdaリソースのTimeout値を確認してみます。
$ aws lambda list-functions --query "Functions[?contains(FunctionName, 'sam-app-HelloWorld ')].[FunctionName, Timeout]" --output table ---------------------------------------------------- | ListFunctions | +--------------------------------------------+-----+ | sam-app-HelloWorldFunction2-15TQGm3NFLJg | 30 | | sam-app-HelloWorldFunction-tOnVGl6K0Z4s | 30 | +--------------------------------------------+-----+
個別に設定していないTimeout値が、どちらのLambdaも30になっています。
最後に
sam delete を実行して、不要なリソースが残らないようにしましょう。
お疲れさまでした。
この記事は私が書きました
嶋田 龍登
記事一覧インフラからアプリのことまでなんでもやりたい、フルスタックを目指すエンジニア