我已经构建了一个 API 网关,它使用 AWS 控制台与 Dynamodb 集成,并将其启动并运行作为概念验证。现在我想获取该 API 网关并通过 CloudFormation 重新生成它以供实际使用。
我已经能够使用 CFT 部署大部分内容,但我仍停留在 AWS::ApiGateway::Method
资源上。具体来说,是Integration
属性。
ResourceHttpGet:
Type: AWS::ApiGateway::Method
Properties:
AuthorizationScopes:
- openid
AuthorizationType: COGNITO_USER_POOLS
AuthorizerId: {'Fn::ImportValue': !Sub '${ProductName}-${TargetEnvironment}-authorizer-customerpool' }
HttpMethod: GET
OperationName: GetFoo
RequestModels:
"application/json": {'Fn::ImportValue': !Sub '${ProductName}-${TargetEnvironment}-apigw-foomodel' }
Integration:
Credentials: !Ref ApiGatewayDynamoDbRole
IntegrationHttpMethod: POST
RequestTemplates:
application/json: !Sub '{ "TableName": "${ProductName}-${TargetEnvironment}-dynamo-primaryapi", "KeyConditionExpression": "UserId = :val", "Limit": 50, "ExpressionAttributeValues": { ":val": { "S": "$context.authorizer.claims.sub" } } }'
Type: AWS
Uri: !Sub "arn:aws:apigateway:${AWS::Region}:dynamodb:action/Query"
ResourceId: !Ref FooResource
RestApiId: {'Fn::ImportValue': !Sub '${ProductName}-${TargetEnvironment}-apigw-primaryapi' }
当我将其上传到 CloudFormation 控制台并运行它时,CF Stack 失败,并在 ResourceHttpGet
资源上出现以下错误:
Invalid ARN specified in the request (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: e6767c61-96dc-461a-ab7c-fd625344b59f; Proxy: null)
我可以注释掉 Integration
属性,并且堆栈会成功部署。我使用了几种不同的资源来帮助验证我使用的 ARN 是否准确。一是a blog post I found关于 API Gateway/Dynamodb 集成和 a second being here在 Stackoverflow 上
有人可以指出我在这里做错了什么吗?为什么它与我提供的 Uri 确认 specification/API requirements 时不一样?如下:
arn:aws:apigateway:region:subdomain.service|service:path|action/service_api. For example, a Lambda function URI follows this form: arn:aws:apigateway:region:lambda:path/path.
我还删除了 !sub
函数的使用,并将 Region 硬编码到 Uri 中,以测试内部函数的使用是否导致了问题,但这并没有造成问题差异。
完全困惑,希望有人能就此向我提供任何指导。
最佳答案
如果您尝试根据手动创建的 API 网关设置创建 CloudFormation 文件,我会查看 AWS SAM 。这更加简洁并且更容易设置。具体来说,我将查看 AWS::Serverless::Api
类型的 DefinitionBody
属性。此属性允许您传入 API 的 openapi 主体定义。
作为起点,您还可以 export your current api使用控制台或文档中指示的其他方法之一。
将这两件事结合起来将使在 CloudFormation 中构建 API 网关变得更加容易。
关于amazon-web-services - 通过 CloudFormation 创建 AWS::ApiGateway::Method 集成时 ARN 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68720219/