在AWS API Gateway中,您可以使用VPC链接将API Gateway与您的Virtual Private Cloud (VPC) 连接起来,并允许您在内部访问API。以下是一个简单的代码示例:
aws apigateway create-vpc-link \
--name "MyVpcLink" \
--target-arns arn:aws:execute-api:region:account-id:api-id/stage-name
aws apigateway create-rest-api \
--name "MyAPI"
aws apigateway create-deployment \
--rest-api-id api-id \
--stage-name "stage-name"
aws apigateway update-stage \
--rest-api-id api-id \
--stage-name "stage-name" \
--patch-operations op=replace,path=/variables/VpcLinkIds,value=vpc-link-id
aws apigateway create-resource \
--rest-api-id api-id \
--parent-id parent-resource-id \
--path-part "my-resource"
aws apigateway put-method \
--rest-api-id api-id \
--resource-id resource-id \
--http-method GET \
--authorization-type "NONE"
aws apigateway create-vpc-link \
--name "MyVpcLink" \
--target-arns arn:aws:execute-api:region:account-id:api-id/stage-name
aws apigateway update-method \
--rest-api-id api-id \
--resource-id resource-id \
--http-method GET \
--patch-operations op=replace,path=/authorizationType,value="AWS_IAM"
aws apigateway update-method \
--rest-api-id api-id \
--resource-id resource-id \
--http-method GET \
--patch-operations op=replace,path=/authorizerId,value=authorizer-id
aws apigateway create-integration \
--rest-api-id api-id \
--resource-id resource-id \
--http-method GET \
--integration-http-method GET \
--type "HTTP_PROXY" \
--uri "http://example.com" \
--connection-type "VPC_LINK" \
--connection-id vpc-link-id
请注意,以上代码示例中的region、account-id、api-id、stage-name、vpc-link-id、parent-resource-id和authorizer-id需要替换为您自己的实际值。
使用上述代码示例,您可以在AWS API Gateway中创建一个与VPC链接关联的API,并将资源和方法与该VPC链接关联,从而允许在内部访问API。