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?

More than 1 year has passed since last update.

-- 下書き

Last updated at Posted at 2023-06-03

DB仕様書がエクセル形式で困ったのでチャットGPTにたのんでみた

もちろん、AWS CloudFormationを使用してLambdaからSESを呼び出してメール送信するためのYAML形式のテンプレートを提供します。

以下がCloudFormationテンプレートの内容です:

SELECT ID,
       "1月の在庫数",
       "2月の在庫数",
       "3月の在庫数",
       "4月の在庫数",
       "5月の在庫数",
       "6月の在庫数",
       "7月の在庫数",
       "8月の在庫数",
       "9月の在庫数",
       "10月の在庫数",
       "11月の在庫数",
       "12月の在庫数"
FROM (
  SELECT ID, 年月, 在庫数
  FROM table1
) AS src
PIVOT (
  MAX(在庫数)
  FOR 年月 IN (
    1 AS "1月の在庫数",
    2 AS "2月の在庫数",
    3 AS "3月の在庫数",
    4 AS "4月の在庫数",
    5 AS "5月の在庫数",
    6 AS "6月の在庫数",
    7 AS "7月の在庫数",
    8 AS "8月の在庫数",
    9 AS "9月の在庫数",
    10 AS "10月の在庫数",
    11 AS "11月の在庫数",
    12 AS "12月の在庫数"
  )
) AS pivoted;

Copy code
AWSTemplateFormatVersion: '2010-09-09'
Description: CloudFormation template to create a Lambda function and IAM role to send emails using SES.

Parameters:
  EmailSenderAddress:
    Description: The sender email address (must be verified in SES)
    Type: String
  EmailRecipientAddress:
    Description: The recipient email address
    Type: String
  SesDomainName:
    Description: The SES domain name (e.g., example.com)
    Type: String

Resources:
  LambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: sts:AssumeRole
      Path: "/"
      Policies:
        - PolicyName: LambdaSESAccessPolicy
          PolicyDocument:
            Statement:
              - Effect: Allow
                Action:
                  - ses:SendEmail
                  - ses:SendRawEmail
                Resource: "*"

  LambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        ZipFile: |
          const AWS = require('aws-sdk');
          const ses = new AWS.SES({ region: 'us-east-1' });  // Change the region if necessary
          exports.handler = async (event) => {
            const params = {
              Source: event.sender,
              Destination: {
                ToAddresses: [event.recipient],
              },
              Message: {
                Subject: {
                  Data: event.subject,
                },
                Body: {
                  Text: {
                    Data: event.body,
                  },
                },
              },
            };
            try {
              await ses.sendEmail(params).promise();
              return {
                statusCode: 200,
                body: 'Email sent successfully!',
              };
            } catch (error) {
              console.error(error);
              return {
                statusCode: 500,
                body: 'Error sending email!',
              };
            }
          };
      Handler: index.handler
      Role: !GetAtt LambdaExecutionRole.Arn
      Runtime: nodejs14.x

Outputs:
  LambdaFunctionArn:
    Description: The ARN of the Lambda function
    Value: !GetAtt LambdaFunction.Arn

このテンプレートでは、以下のパラメータを指定する必要があります:

EmailSenderAddress: メールを送信する際の差出人のメールアドレスです。SESで事前に確認済みのアドレスである必要があります。
EmailRecipientAddress: メールの宛先となるメールアドレスです。
SesDomainName: SESで設定した独自ドメイン名を指定します(例: example.com)。
テンプレートはIAMロールとLambda関数を作成し、Lambda関数で指定した差出人と宛先のメールアドレスを使用してSESを呼び出してメールを送信します。SESのリージョンは「us-east-1」として設定していますが、必要に応じて適切なリージョンに変更してください。

このCloudFormationテンプレートをAWS Management ConsoleのCloudFormationセクションにコピー&ペーストし、スタックを作成してください。必要なパラメータを入力し、実行してください。これにより、Lambda関数と必要なIAMロールが作成され、指定した差出人と宛先のメールアドレスを使用してSESからメールが送信されます。

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?