LoginSignup
2
0

More than 1 year has passed since last update.

SAMポリシーテンプレートのサンプル

Posted at

AWS SAMを利用するとAPIGateway, Lambdaの定義をシンプルに
シンプルに記載することが出来ます。

今回はAWSが提供しているAWS SAMポリシーテンプレートのご紹介です。
DynamoDBやS3の読み込み、書き込みなどについてよくあるパターンを
テンプレート化してくれており大変便利です。
かなりの範囲をサポートしてくれているので、個別IAMポリシーを作成する
場面はかなり少なくなると思います。

用意されているテンプレートリストは以下の公式情報を参照ください。

AWS SAMポリシーテンプレート

使い方は簡単でポリシーを指定し、プレースホルダ値を設定するだけです。
以下の場合は、「TableName」と「BucketName」がプレースホルダ値です。

      - DynamoDBCrudPolicy:
          TableName: yourTableName
      - S3CrudPolicy:
          BucketName: !GetAtt S3Stack.Outputs.MyBucketName

SAMポリシーテンプレートサンプル

メイン側でLambda+APIGatewayの定義、
template_s3.yamlでS3の定義を行い、ネスト参照しています。

template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  sam-app

Resources:

  # S3バケット定義(template_s3.yamlで定義)
  S3Stack:
    Type: AWS::Serverless::Application
    Properties:
      Location: template_sub/template_s3.yaml
      Parameters:
        # template_s3.yamへパラメータを連携
        StackName: !Ref AWS::StackName

  # APIGateway定義
  ApiGatewayApps:
    Type: AWS::Serverless::Api
    Description: MyApiGatewayDemo
    Properties:
      Name: MyApiGatewayDemo
      StageName: dev
      EndpointConfiguration:
        Type: REGIONAL

  # Lambad関数定義
  AddFunc:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: AddFunc
      CodeUri: AddFunc/
      Handler: app.lambda_handler
      Runtime: python3.8
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /add
            Method: get
      Events:
        AddFuncEvent:
          Type: Api
          Properties:
            Path: /add
            Method: POST
            RestApiId: !Ref ApiGatewayApps
      Policies:
      # AWS管理ポリシー
      - AmazonDynamoDBReadOnlyAccess
      # SAM Policy Template
      - DynamoDBCrudPolicy:
          TableName: yourTableName
      - S3CrudPolicy:
          BucketName: !GetAtt S3Stack.Outputs.MyBucketName
      - S3ReadPolicy:
          BucketName: !GetAtt S3Stack.Outputs.MyBucketName
      # インラインポリシー
      - Statement:
        - Sid: SESSendMailPolicy
          Effect: Allow
          Action:
          - ses:SendEmail
          - ses:SendRawEmail
          Resource: '*'
template_sub/template_s3.yaml
Parameters:
  StackName:
    Type: String

Resources:
  # S3バケット定義
  MyBucket:
    Type: "AWS::S3::Bucket"
     # スタック削除時にバケット削除したく無い場合は指定
#    DeletionPolicy: Retain
     # リソースの置時にリソースを保持したい場合は指定
#    UpdateReplacePolicy: Retain
    Properties:
      BucketName: my-bucket-sample-2021mmdd
      AccessControl: Private
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        BlockPublicPolicy: true
        IgnorePublicAcls: true
        RestrictPublicBuckets: true
      LifecycleConfiguration:
        Rules:
          - ExpirationInDays: 7
            Id: MyBucketLifecycleRule
            Status: "Enabled"
            Prefix: fileupload/

Outputs:
  MyBucketName:
    # バケット名を共有
    Value: !Ref MyBucket

2
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
2
0