AWS Lambda から Amazon Document DB に接続した

VPC を作成

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/364773/5f3d9bd1-85ea-4954-ba5f-f23684c940f4.png)



セキュリティグループを作成

ec2

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/364773/20ea3413-7761-5068-1590-71c7323e5733.png)


document-db

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/364773/4e2352b8-3f13-f517-75cb-d0e438072407.png)



サブネットグループの作成

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/364773/5aa54713-4105-f11c-b66b-8925f9c4677e.png)

document db 作成

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/364773/9813b55c-0bce-28f7-37fa-999255a1ce82.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/364773/364ecd0f-7621-5755-6b6c-3e010d35a864.png)

ec2 を作成

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/364773/c005e5e0-8c7e-fb90-7674-d280b257b6ef.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/364773/07b9aa04-6b98-d870-2e71-fba2b083ff66.png)

ec2 に mongo シェルをインストール

https://docs.aws.amazon.com/ja_jp/documentdb/latest/developerguide/connect-ec2.html#connect-ec2.install-mongo-shell

接続

https://docs.aws.amazon.com/ja_jp/documentdb/latest/developerguide/connect-ec2.html#connect-ec2.connect-use

```
connecting to: mongodb://docdb-xxxxxxxxxxx.docdb.amazonaws.com:27017/?gssapiServiceName=mongodb
2022-06-28T00:14:02.722+0000 W NETWORK  [thread1] Failed to connect to 10.0.150.226:27017 after 5000ms milliseconds, giving up.
2022-06-28T00:14:02.722+0000 E QUERY    [thread1] Error: couldn't connect to server docdb-2022-06-27-22-46-04.cluster-c9htjlshc7qa.ap-northeast-1.docdb.amazonaws.com:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:275:13
@(connect):1:6
exception: connect failed
```

セキュリティグループの設定がおかしかったことが原因

https://docs.aws.amazon.com/documentdb/latest/developerguide/troubleshooting.connecting.html#troubleshooting.cannot-connect.inbound-not-allowed:~:text=Reservations%5B*%5D.Instances%5B*%5D.VpcId%27-,%E3%82%BB%E3%82%AD%E3%83%A5%E3%83%AA%E3%83%86%E3%82%A3%E3%82%B0%E3%83%AB%E3%83%BC%E3%83%97%E3%81%8C%E3%82%A4%E3%83%B3%E3%83%90%E3%82%A6%E3%83%B3%E3%83%89%E6%8E%A5%E7%B6%9A%E3%82%92%E3%83%96%E3%83%AD%E3%83%83%E3%82%AF%E3%81%99%E3%82%8B,-Amazon%20DocumentDB%E3%82%AF%E3%83%A9%E3%82%B9%E3%82%BF

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/364773/2f07012c-8a9c-2264-4756-46611d46436d.png)

document db にあてているセキュリティーグループのインバウンドルールのポートを修正した

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/364773/5465bbcf-d74d-8b10-d057-e318ff152b21.png)


## Lambda を API で叩く

https://dev.classmethod.jp/articles/aws-lambda-function-urls-built-in-https-endpoints/

## Lambda から Document DB に接続

TLS が有効な場合と無効な場合の接続方法が公式ドキュメントにあった

https://docs.aws.amazon.com/ja_jp/documentdb/latest/developerguide/connect_programmatically.html#connect_programmatically-tls_disabled

TLS が有効な場合、うまく接続できなかったのでとりあえず、無効にした

https://qiita.com/hiro-tech1192/items/b85ce3ef9fde524be117#%EF%BC%95%EF%BC%92%E3%83%91%E3%83%A9%E3%83%A1%E3%83%BC%E3%82%BF%E3%82%B0%E3%83%AB%E3%83%BC%E3%83%97%E3%81%AE%E4%BD%9C%E6%88%90tls%E7%84%A1%E5%8A%B9%E5%8C%96

Lambda の VPC を設定

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/364773/0f35e2dc-4001-ffd0-4e09-0059604e3189.png)