2
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?

AWS API Gateway のリソースポリシーでクロスアカウントアクセスを簡単に実現!

Posted at

こんにちは!最近、API Gatewayを使ってクロスアカウントでAPIを投げる必要があったのですが、IAMロールを適切に設定すれば簡単に実現できることが分かったので、やり方をまとめます!(ちなみに、今までIAMロールに関しても全然知らない状態でした…)

なぜクロスアカウントで API を投げるのか?

AWS では、異なるアカウント間でリソースを安全に連携することがよくあります。
例えば今回の場合、以下の状況でした!

  • アカウント A(API をホスト) → API Gateway + Lambda でデータ提供
  • アカウント B(クライアント側) → API Gateway にリクエストを送信

しかし、デフォルトでは他のAWSアカウントからAPI Gatewayを実行することはできません!😱

そこで、IAMロールを活用してクロスアカウントアクセスを設定すれば、安全にAPIを実行できます!💪

API Gateway のリソースポリシーを設定

まず、API Gatewayのリソースポリシーを設定し、アカウントBのIAM ユーザー/ロールを許可します。

:point_up:API Gateway のリソースポリシー(アカウント A に設定)

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::222222222222:role/APIGatewayInvokerRole"
                ]
            },
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:ap-northeast-1:111111111111:abcd1234/*"
        }
    ]
}
  • "AWS": "arn:aws:iam::222222222222:role/APIGatewayInvokerRole"
    → アカウント B(222222222222)の IAM ロール APIGatewayInvokerRole を許可
  • "Action": "execute-api:Invoke"
    → API を実行する権限
  • "Resource": "arn:aws:execute-api:ap-northeast-1:111111111111:abcd1234/*"
    → API Gateway のリソース ARN を指定(abcd1234 は API Gateway の ID)

アカウントBにIAMロールを作成

次に、アカウント B(API を実行する側) に IAM ロールを作成し、API Gateway へのアクセス権限を付与 します。

:point_up: IAMロールの作成
AWS コンソールで IAM を開く(アカウント B)
「ロール」→「ロールの作成」
「カスタム信頼ポリシーを設定」
信頼関係(トラストポリシー)を設定
API Gateway があるアカウント(アカウント A)を信頼する

:v: IAM ロールの信頼ポリシー

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::111111111111:root"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}
  • "AWS": "arn:aws:iam::111111111111:root"
    → アカウント A(API Gateway のあるアカウント)を信頼
  • Action": "sts:AssumeRole"
    → アカウント A がこのロールを引き受けられるようにする
  • アクセスポリシー(API Gateway へのアクセス権限)
    次に、この IAM ロールに API Gateway へのアクセスを許可するポリシーを付与します。
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:ap-northeast-1:111111111111:abcd1234/*"
        }
    ]
}
  • "Action": "execute-api:Invoke"
    → API Gateway を実行する権限
  • "Resource": "arn:aws:execute-api:ap-northeast-1:111111111111:abcd1234/*"
    → アカウントAのAPI Gatewayにアクセスできるようにする

アカウントBからAPIを実行

今回はPostmanで実行してみたいと思います!

  1. Postman を開く
    「新しいリクエストを作成」
    HTTP メソッド: GET または POST
    例えばのURL: https://abcd1234.execute-api.ap-northeast-1.amazonaws.com/prod/myapi

  2. 「Authorization」タブを開く
    Type: AWS 署名
    Access Key: IAM ユーザー(ApiInvokerUser)のアクセスキー
    Secret Key: IAM ユーザー(ApiInvokerUser)のシークレットキー
    AWS Region: ap-northeast-1
    Service Name: execute-api

    image.png

  3. 「Send」をクリックして API を実行!

まとめ

:point_right:API Gatewayのリソースポリシーを設定し、クロスアカウントでのアクセスを許可
:point_right:アカウントBにIAM ロールを作成し、API Gatewayを実行できるようにする
:point_right:IAM ロールを使ってPostman経由でAPIを実行!

+)雑談
未経験のエンジニアとして、初めて自分が一からawsの認証周りを考える機会でした!最初はなんだかんだ怖いし、分からないことだらけでしたが、意外と楽しかったです!(もちろん色んな先輩にアドバイスを聞いてました)
これからも頑張りましょうー!

2
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
2
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?