1
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?

More than 1 year has passed since last update.

SAMで環境別にデプロイ

Last updated at Posted at 2023-01-02

やりたいこと

samで構築されるリソースを各環境(dev/prod)ごとに分けたい。

環境別に分ける方法

  • samconfig.tomlを環境別に分けて記述する。
  • template.yamlにパラメータとして環境情報を渡して、環境別に各リソース名を切り替える

構築するリソース

  • S3
  • Lambda
  • DynamoDB

template.yaml

Parametersで外部から環境情報を渡せるようにし、各リソース名を環境ごとに切り替える。
※関連個所のみ記載

Parameters:
  Env:
    Type: String
    AllowedValues:
      - dev
      - prod

Resources:
  CreateThumbnailBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub create-thumbnail-test-${Env}
  CreateThumbnailFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: create_thumbnail/
      Handler: app.lambda_handler
      Runtime: python3.9
      FunctionName: !Sub create-thumbnail-${Env}
      Architectures:
        - x86_64
      Environment: 
        Variables:
          ENV: !Ref Env
      Policies: 
        - DynamoDBCrudPolicy:
            TableName: !Ref CreateThumbnailTable
      Events:
        S3Input:
          Type: S3
          Properties:
            Bucket: !Ref CreateThumbnailBucket
            Events: s3:ObjectCreated:*
            Filter:
              S3Key:
                Rules:
                  - Name: prefix
                    Value: input/
  CreateThumbnailTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: !Sub create-thumbnail-${Env}
      BillingMode: PAY_PER_REQUEST
      AttributeDefinitions: 
        - AttributeName: id
          AttributeType: S
      KeySchema: 
        - AttributeName: id
          KeyType: HASH

環境変数を使用することで、Lambda処理内で環境の判別が可能となる。

      Environment: 
        Variables:
          ENV: !Ref Env

Lambda(Python)では設定した環境変数を以下で取得できる。

os.environ['ENV']

samconfig.toml

環境ごとにデプロイ情報を記述。
parameter_overridesの設定値が、template.yamlのParametersに渡される。

version = 0.1
[dev]
[dev.deploy]
[dev.deploy.parameters]
stack_name = "sam-app-dev"
s3_bucket = "aws-sam-cli-managed-default-samclisourcebucket-xxxxxx"
s3_prefix = "sam-app-dev"
region = "ap-northeast-1"
confirm_changeset = true
capabilities = "CAPABILITY_IAM"
image_repositories = []
parameter_overrides = [
  "Env=dev"
]

[prod]
[prod.deploy]
[prod.deploy.parameters]
stack_name = "sam-app-prod"
s3_bucket = "aws-sam-cli-managed-default-samclisourcebucket-xxxxxx"
s3_prefix = "sam-app-prod"
region = "ap-northeast-1"
confirm_changeset = true
capabilities = "CAPABILITY_IAM"
image_repositories = []
parameter_overrides = [
  "Env=prod"
]

デプロイ方法

dev

sam deploy --config-env dev

prod

sam deploy --config-env prod

ローカル実行時に環境変数を設定

環境変数ファイルを作成

{
  "CreateThumbnailFunction" :{
    "ENV": "dev"
  }
}

作成したファイルを--env-varsで指定して実行

sam local invoke --config-env dev
1
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
1
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?