1
1

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 3 years have passed since last update.

AWSのRDSのPostgreSQLでAWS Lambdaを呼ぶ

Last updated at Posted at 2021-08-31

目的

PostgreSQLからAWS Lambdaが呼び出せるのでやってみます。
やり方は本家のドキュメントの通りです。

コード

AWS Lambda

hander.rb
require 'json'

def index(event:, context:)
  {
    statusCode: 200,
    body: {
      message: 'Go Serverless v1.0! Your function executed successfully!',
      input: event
    }.to_json
  }
end

IAM

IAM Policy

aws iam create-policy --policy-name rds-lambda-policy  --policy-document '{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AllowAccessToExampleFunction",
            "Effect": "Allow",
            "Action": "lambda:InvokeFunction",
            "Resource": "arn:aws:lambda:us-east-1:12345:function:pg-lambda-sample-api-staging-index"
        }
    ]
}'

IAM Role

aws iam create-role --role-name rds-lambda-role --assume-role-policy-document '{
    "Version": "2012-10-17",
    "Statement": [
        {
        "Effect": "Allow",
        "Principal": {
            "Service": "rds.amazonaws.com"
        },
        "Action": "sts:AssumeRole"
        }
    ] 
}'

Attach

aws iam attach-role-policy --policy-arn arn:aws:iam::12345:policy/rds-lambda-policy  --role-name rds-lambda-role

RDSに設定

aws rds add-role-to-db-instance \
--db-instance-identifier db-instance-name \
--feature-name Lambda \
--role-arn arn:aws:iam::12345:role/rds-lambda-role \
--region us-east-1

PostgreSQL

準備

CREATE EXTENSION IF NOT EXISTS aws_lambda CASCADE;

実行

SELECT * FROM aws_lambda.invoke(
  'arn:aws:lambda:us-east-1:12345:function:pg-lambda-sample-api-staging-index', 
  '{"body": "Hello from Postgres!"}'::json
);

-[ RECORD 1 ]----+-----------------------------------------------------------------------------------------------------------------------------------------------------
status_code      | 200
payload          | {"statusCode":200,"body":"{\"message\":\"Go Serverless v1.0! Your function executed successfully!\",\"input\":{\"body\":\"Hello from Postgres!\"}}"}
executed_version | $LATEST
log_result       | 

非同期実行

SELECT * FROM aws_lambda.invoke(
  'arn:aws:lambda:us-east-1:12345:function:pg-lambda-sample-api-staging-index', 
  '{"body": "Hello from Postgres!"}'::json,
  'us-east-1',
  'Event'
);

-[ RECORD 1 ]----+----
status_code      | 202
payload          | 
executed_version | 
log_result       |

ログも取得

SELECT *, 
  convert_from(decode(log_result, 'base64'), 'utf-8') as log FROM 
  aws_lambda.invoke('arn:aws:lambda:us-east-1:12345:function:pg-lambda-sample-api-staging-index', 
  '{"body": "Hello from Postgres!"}'::json, 
  'us-east-1', 
  'RequestResponse', 
  'Tail'
);

-[ RECORD 1 ]----+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
status_code      | 200
payload          | {"statusCode":200,"body":"{\"message\":\"Go Serverless v1.0! Your function executed successfully!\",\"input\":{\"body\":\"Hello from Postgres!\"}}"}
executed_version | $LATEST
log_result       | U1RBUlQgUmVxdWVzdElkOiAzY2RiMDAzYi0yNWFjLTQ4MjItOTQzZS1lNWMxNjhlNzM1ODUgVmVyc2lvbjogJExBVEVTVApFTkQgUmVxdWVzdElkOiAzY2RiMDAzYi0yNWFjLTQ4MjItOTQzZS1lNWMxNjhlNzM1ODUKUkVQT1JUIFJlcXVlc3RJZDogM2NkYjAwM2ItMjVhYy00ODIyLTk0M2UtZTVjMTY4ZTczNTg1CUR1cmF0aW9uOiAxLjY1IG1zCUJpbGxlZCBEdXJhdGlvbjogMiBtcwlNZW1vcnkgU2l6ZTogMTAyNCBNQglNYXggTWVtb3J5IFVzZWQ6IDQ0IE1CCQo=
log              | START RequestId: 3cdb003b-25ac-4822-943e-e5c168e73585 Version: $LATEST                                                                                                                                                                                                                                                                                          +
                 | END RequestId: 3cdb003b-25ac-4822-943e-e5c168e73585                                                                                                                                                                                                                                                                                                             +
                 | REPORT RequestId: 3cdb003b-25ac-4822-943e-e5c168e73585  Duration: 1.65 ms       Billed Duration: 2 ms   Memory Size: 1024 MB    Max Memory Used: 44 MB                                                                                                                                                                                                          +
                 |
1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?