AWSのRDSのPostgreSQLでAWS Lambdaを呼ぶ

# 目的
PostgreSQLからAWS Lambdaが呼び出せるのでやってみます。
やり方は本家の[ドキュメント](https://docs.aws.amazon.com/ja_jp/AmazonRDS/latest/UserGuide/PostgreSQL-Lambda.html#PostgreSQL-Lambda-invoke)の通りです。

# コード

## AWS Lambda
```ruby: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
```bash
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
```bash
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
```bash
aws iam attach-role-policy --policy-arn arn:aws:iam::12345:policy/rds-lambda-policy  --role-name rds-lambda-role
```

### RDSに設定
```bash
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

### 準備
```sql
CREATE EXTENSION IF NOT EXISTS aws_lambda CASCADE;
```

### 実行
```sql
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       | 
```

### 非同期実行
```sql
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       |
```

### ログも取得
```sql
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                                                                                                                                                                                                          +
                 |
```