【AWS】Python Lambdaのdeploy - CloudFormation

【AWS】Python Lambdaのdeploy - CloudFormation:

【AWS】Python Lambdaのdeploy - CloudFormation - Qiita
【AWS】Python Lambdaのdeploy - Chalice - Qiita

CloudFormationを使えば、AWSの様々な設定やdeployを自動化できます。AWS Serverless Application Model (SAM) は、AWS が公式で提供しているサーバーレスアプリケーションのためのフレームワークです。Lambda, API Gateway, DynamoDB,Cognito,S3等、AWSのほとんどのリソースをひとまとめに管理 (作成 / 更新 / 削除) することができます。一部設定できない項目もあります。SAMはyamlファイルを書くことで定義できます。(JSONでも可能ですがここでは触れません)

定刻に定型のメールを送信するLambda関数をdeployすることにします。


1.SAMファイル

まずは以下のようなyamlを書きます。Globalsで「Runtime: python3.6」を指定していることに注意してください。中にコメントしますので、コピペするときは削除してください。またCloudWatch のEventsでcronを定義し、スケジューラを設定することに注意してください。

serverless.yml
AWSTemplateFormatVersion: "2010-09-09" 
Transform: AWS::Serverless-2016-10-31 
Globals: 
  Function: 
    Runtime: python3.6   # PythonのLambda関数 
    Timeout: 30 
    Tracing: Active 
    Environment: 
      Variables: 
        REGION: !Ref AWS::Region 
Description: "Example template including Lambda" 
Resources: 
  amazonsessample: 
    Type: AWS::Serverless::Function    # Lambdaのリソース 
    Properties: 
      Handler: amazonsessample.handler  # Handler 
      Policies: 
      - Version: '2012-10-17' 
        Statement: 
        - Effect: Allow 
          Action: 
            - ses:SendEmail      # SESのSendEmailを使う 
          Resource: 
            - '*'  
      Events:                    # CloudWatch のEventsでcronを定義 
        ScheduleEvent: 
          Type: Schedule 
          Properties: 
            Schedule: cron(45 1 * * ? *) 


2.Lambda関数

ここでは次のドキュメントのソースを利用します。「AWS SDK for Python (Boto) を使用して E メールを送信する - AWS公式ドキュメント」

PythonからAWSへアクセスするためには、「AWS SDK for Python (Boto3) - AWS公式ドキュメント」というライブラリを使います。

amazonsessample.py
import boto3 
from botocore.exceptions import ClientError 
 
def handler(event, context): 
    print("#####",event) 
    # -- SENDERとRECIPIENTのemailは予めSESでVerifyする必要がある 
    SENDER = "Sender Name <verified-email1@gmail.com>" 
    RECIPIENT = "verified-email2@gmail.com" 
 
    AWS_REGION = "us-west-2"   # オレゴンを設定。SESでは東京は使えない。 
    SUBJECT = "Amazon SES Test (SDK for Python)" 
 
    BODY_TEXT = ("Amazon SES Test (Python)\r\n" 
                 "This email was sent with Amazon SES using the " 
                 "AWS SDK for Python (Boto)." 
                ) 
 
    BODY_HTML = """<html> 
<head></head> 
<body> 
  <h1>Amazon SES Test (SDK for Python)</h1> 
  <p>This email was sent with 
    <a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the 
    <a href='https://aws.amazon.com/sdk-for-python/'> 
      AWS SDK for Python (Boto)</a>.</p> 
</body> 
</html> 
"""     
 
    CHARSET = "UTF-8" 
    client = boto3.client('ses',region_name=AWS_REGION) 
 
    try: 
        response = client.send_email( 
            Destination={ 
                'ToAddresses': [ 
                    RECIPIENT, 
                ], 
            }, 
            Message={ 
                'Body': { 
                    'Html': { 
                        'Charset': CHARSET, 
                        'Data': BODY_HTML, 
                    }, 
                    'Text': { 
                        'Charset': CHARSET, 
                        'Data': BODY_TEXT, 
                    }, 
                }, 
                'Subject': { 
                    'Charset': CHARSET, 
                    'Data': SUBJECT, 
                }, 
            }, 
            Source=SENDER, 
        ) 
 
    except ClientError as e: 
        print(e.response['Error']['Message']) 
    else: 
        print("Email sent! Message ID:"), 
        print(response['ResponseMetadata']['RequestId']) 
 
    return 9    # 適当なreturn値 
最後にboto3をインストールしておきましょう。

pip install boto3 


3.deployと削除

deployにはaws cloudformationを2つ打つだけなのですが、以下のようにshスクリプトにしておいた方が便利でしょう。ここで重要なのはstack-nameの指定です。deployはこのstack-nameのもとで行われます。またS3上にcf-template-xxxxxという(名前は任意)bucketをあらかじめ用意しておく必要があります。

go1.sh
#!/bin/bash -e 
# *** deploy cloudformation yaml 
aws cloudformation package --template-file serverless.yml --output-template-file serverless-output.yml --s3-bucket cf-template-xxxxx 
aws cloudformation deploy --template-file serverless-output.yml --stack-name mystackname --capabilities CAPABILITY_IAM 
deployした全てのリソースは、CloudFormationサービスでstack-nameを指定して、一括して削除できます。

今回は以上です。

コメント

このブログの人気の投稿

投稿時間:2021-06-17 05:05:34 RSSフィード2021-06-17 05:00 分まとめ(1274件)

投稿時間:2021-06-20 02:06:12 RSSフィード2021-06-20 02:00 分まとめ(3871件)

投稿時間:2020-12-01 09:41:49 RSSフィード2020-12-01 09:00 分まとめ(69件)