amazon-web-services - 在cloudformation中输出api键值

标签 amazon-web-services aws-cloudformation aws-api-gateway

我有一个输出变量的cloudformation模板。输出变量之一是

 ApiGKeyId:
    Description: "Api Key Id"
    Value: !Ref ApplicationApiGatewayApiKey

这将返回 API 网关 key 的 ID,而不是实际值。有没有办法获取该值?

最佳答案

根据以下线程,不支持属性“Value”~
https://github.com/awslabs/serverless-application-model/issues/206

第三方维护可用属性一目了然: https://theburningmonk.com/cloudformation-ref-and-getatt-cheatsheet/

经过一番研究,我觉得除了使用自定义资源调用 lambda 函数之外,没有其他方法可以检索 ApiKey 的值。这是我的示例代码。

#######################################################
##### Start of Custom functions #####
#######################################################
ValueFunc:
  Type: AWS::Lambda::Function
  Properties:
    Code:
      ZipFile: >
        var response = require('cfn-response');
        var AWS = require('aws-sdk');

        exports.handler = function(event, context) {
          var apiKeyID = event.ResourceProperties.ApiKeyID;
          var apigateway = new AWS.APIGateway();
          var params = {
            apiKey: apiKeyID,
            includeValue: true
          };

          apigateway.getApiKey(params, function(err, ApiKeyData) {
            if (err) {
              console.log(err, err.stack); // an error occurred
              var responseData = { "mykey" : "error reading ApiKey" };
              response.send(event, context, response.SUCCESS, responseData);
            } else {
              console.log(ApiKeyData.value);      // successful response
              var responseData = { "mykey" : ApiKeyData.value };
              response.send(event, context, response.SUCCESS, responseData);
            }
          });
        };
    Handler: index.handler
    Runtime: nodejs8.10
    Timeout: 30
    Role: !Sub "arn:aws:iam::${AWS::AccountId}:role/${LambdaExecutionRole}"
GetApiKeyValue:
  Type: Custom::LambdaCallout
  Properties:
    ServiceToken: !GetAtt ValueFunc.Arn
    ApiKeyID: !Ref ApiKey

关于amazon-web-services - 在cloudformation中输出api键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58548895/

相关文章:

amazon-web-services - AWS CloudFormation 与 Web 控制台?

amazon-web-services - 如何使用 aws cloudformation 模板在 aws cognito 用户池中设置所需属性?

amazon-web-services - 在其他区域部署 AWS-CDK 堆栈

aws-api-gateway - 丢失数据如何在 cloudwatch 警报中工作?

node.js - 在 Amazon Elastic BeansTalk 中部署 node.js 应用程序

java - FusionAuth 与 AWS Elasticsearch 的不完整重新索引

amazon-web-services - AWS Cloudformation 模板配置无效

python - 通过 API Gateway 在 EC2 上运行函数

amazon-web-services - 如何从 Amazon API Gateway 将查询字符串或路由参数传递到 AWS Lambda

mysql - [root@localhost ~]# mysql -u root -p 输入密码 : ERROR 1045 (28000): Access denied for user 'root' @'localhost' (using password: YES)