5
8

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 Amplify Function でプライベートネットワーク内のRDSに接続する方法

Last updated at Posted at 2021-06-26

この記事について

Amplify を用いると簡単にバックエンド環境を揃えられるので、
スピーディ且つスケーラブルなアプリ開発にもってこいですよね。

しかし、アプリがスケールしてきて、応用的な開発をしたいときに、
公式ドキュメントが英語だったり、日本語の記事がまだ豊富でないので詰まったりすることが良くあります。

今回は、Amplify CLI でVPC内のRDSに接続するために、VPCリソースを利用するLambda 関数を作成する手順について解説します。

コンソールからVPCリソースを利用するLambda関数の作成方法は少し調べれば分かりますが、
Apmplify CLI でLambda関数を作成する場合、バックエンドリソースは Cloud Formation で管理されるので、
<関数名>-cloudformation-template.json ファイルを手動で編集する必要性があります。

cloudformation-template.json のプロパティ追加

こちらのGitHubIssueを参考にしました。

Deploy lambda to VPC. #32
https://github.com/aws-amplify/amplify-cli/issues/32

amplify/backend/function/<関数名>/<関数名>-cloudformation-template.json を編集します。
追加するプロパティは”VpcConfig"のSecurityGroupIdsSubnetIdsです。

sg-xxx と subnet-xxx をご自身のセキュリティグループとサブネットのIDをセットしてください。

"Resources": {
    "LambdaFunction": {
        "Type": "AWS::Lambda::Function",
        "Metadata": {
            "aws:asset:path": "./src",
            "aws:asset:property": "Code"
        },
        "Properties": {
            "Handler": "index.handler",
              ....
            "VpcConfig": {
                "SecurityGroupIds": [
                    "sg-xxx"
                ],
                "SubnetIds": [
                    "subnet-xxx",
                    "subnet-xxx",
                    "subnet-xxx"
                ]
            }
        },
        "LambdaExecutionRole": {
              ....
        

これでamplify push functionを実行!

しかし、これだけだと EC2 の権限周りらしいというエラーメッセージをだしてデプロイ失敗、、、となります。

An error occurred (InvalidParameterValueException) when calling the CreateFunction operation: The provided execution role does not have permissions to call CreateNetworkInterface on EC2

上記エラーの解消法にはこちらの記事が参考になりました。
LambdaのIAMロールにVPCアクセスの権限がないためエラーになっているようです。
{関数名}-template.json内のLambdaのIAMロールポリシーに以下を追加すれば解決しました。

"lambdaVPCexecutionpolicy": {
      "DependsOn": [
        "LambdaExecutionRole"
      ],
      "Type": "AWS::IAM::Policy",
      "Properties": {
        "PolicyName": "lambda-vpc-access-execution-policy",
        "Roles": [
          {
            "Ref": "LambdaExecutionRole"
          }
        ],
        "PolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents",
                "ec2:CreateNetworkInterface",
                "ec2:DescribeNetworkInterfaces",
                "ec2:DeleteNetworkInterface",
                "ec2:AssignPrivateIpAddresses",
                "ec2:UnassignPrivateIpAddresses"
              ],
              "Resource": "*"
            }
          ]
        }
      }
    },

どなたか同じ悩みをもった方のご参考になれば幸いです。

5
8
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
5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?