0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AWS SAM は リソース(Type)毎に Tags の設定方法が違う

Last updated at Posted at 2025-02-28

AWS SAM の template.yaml を設定していたところ、Function と Role で Tags の設定方法が違っていたので、公式ドキュメントを確認して整理しました。

まず、設定方法の一覧をお伝えした後に、実際の template.yaml への設定方法を見ていきます。

Tags の設定方法

リソースタイプ マップ形式 キーバリューペア形式
AWS::Serverless::Api
AWS::Serverless::Application
AWS::Serverless::Function
AWS::Serverless::GraphQLApi
AWS::Serverless::HttpApi
AWS::Serverless::SimpleTable
AWS::Serverless::StateMachine
その他(AWS::IAM::Role 等)

template.yaml の書き方

下記の例では、AWS::Serverless::Function と AWS::IAM::Role に、それぞれ Key: Project, Value: MyProject と Key: Environment, Value: dev という2つのタグを設定しています。

AWS::Serverless::Function の場合、Tags はマップ形式

template.yaml
Resources:
  FooFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: fooFunction
      CodeUri: foo/
      Role: !GetAtt FooRole.Arn
      Timeout: 3
      # タグはマップ形式
      Tags:
        Project: MyProject
        Environment: dev

その他のリソースの場合、Tags はキーバリューペア形式

template.yaml
Resources:
  ...(省略)...
  FooRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action: sts:AssumeRole
      Policies:
        - PolicyName: LambdaBasicExecution
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Resource: "*"
      # タグはキーバリューペア形式
      Tags:
        - Key: Project
          Value: MyProject
        - Key: Environment
          Value: dev

設定方法が違う理由

AWS SAM の各リソースの Type は、CloudFormation のラッパーとして定義されているようです。
なので、同じ Lambda ファンクション でも、AWS::Lambda::Function はキーバリューペア形式ですが、それを拡張した AWS::Serverless::Function はマップ形式がサポートされるようになったようです。

参考サイト

公式ドキュメントも併せて確認してみてください。

下記の関連記事もどうぞ!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?