AWS API Gateway可通过授权(authorization)和资源策略(resource policy)配置实现跨账户访问。具体步骤如下:
创建一个API Gateway和至少一个API,假设名为“my-api”,其中包含一个资源路由“/my-resource”。
为API Gateway创建一个用于授权的Lambda函数,该函数将验证来自于访问者的 AWS 访问密钥(access key)或身份验证 token,以确保他们有权访问API。Lambda函数的返回结果必须包含类似于以下内容的信息:
{ "principalId": "user|a-backend-account", "policyDocument": { "Version": "2012-10-17", "Statement": [{ "Action": "execute-api:Invoke", "Effect": "Allow", "Resource": "arn:aws:execute-api:us-east-1:an-api-gateway-endpoint///my-resource" }] } }
其中“user|a-backend-account”即为授权后的访问者账户ID,这个随具体的授权方式而定。
{ "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Principal": {"AWS": "arn:aws:iam::a-backend-account:root"}, "Action": "execute-api:Invoke", "Resource": "arn:aws:execute-api:us-east-1:an-api-gateway-endpoint///my-resource" }] }
其中“a-backend-account”即为允许访问的账户ID。