【问题标题】:exporting outputs in cloudformation using custom resources with lambda使用带有 lambda 的自定义资源以 cloudformation 导出输出
【发布时间】:2020-11-03 04:59:30
【问题描述】:

我使用自定义资源运行 CFN 模板来导出 route53 私有托管区域名称。我能够根据需要导出托管区域名称,但无法将此托管区域名称导入另一个堆栈。 我收到此错误:

Value of property HostedZoneName must be of type String

感谢任何帮助。谢谢。 这是我的代码。

AWSTemplateFormatVersion: 2010-09-09
Description: CloudFormation exports


Resources:
  LambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - lambda.amazonaws.com
          Action:
          - sts:AssumeRole
      Path: "/"
      Policies:
      - PolicyName: root
        PolicyDocument:
          Version: '2012-10-17'
          Statement:
          - Effect: Allow
            Action:
            - "logs:CreateLogGroup"
            - "logs:CreateLogStream"
            - "logs:PutLogEvents"
            - "route53:Get*"
            - "route53:List*"
            - "route53:TestDNSAnswer"
            Resource: "*"
  GetCertARN: 
    Type: "AWS::Lambda::Function"
    DeletionPolicy: Delete
    DependsOn:
      - LambdaExecutionRole
    Properties: 
      Handler: "index.handler"
      Role: 
        Fn::GetAtt: 
          - "LambdaExecutionRole"
          - "Arn"
      Runtime: "python3.7"
      MemorySize: 128
      Timeout: 100
      Code: 
        ZipFile: |
          import boto3
          import botocore
          import cfnresponse
          route53 = boto3.client('route53')
          def handler(event, context):
            hostedZoneName=''
            response2 = route53.list_hosted_zones()
            f=response2['HostedZones']
            for zone in f:
                config=zone["Config"]
                name=zone["Name"]
                e=config["PrivateZone"]
                if ('abcxyz.cloud' in name) and e:
                    hostedZoneName=name
            responseData = {}
            responseData['hostedzonename'] = hostedZoneName
            cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
  CertArnInvocation:
    Type: Custom::CertArn
    Properties:
      ServiceToken: !GetAtt GetCertARN.Arn
      Region: !Ref "AWS::Region"

Outputs:
  ExportsStackName:
    Value: !Ref 'AWS::StackName'
    Export:
      Name: !Sub '${AWS::StackName}'

  HostedZoneNameOutput: 
    Value: !GetAtt CertArnInvocation.hostedzonename
    Description: Return Value of private hosted zone name
    Export: 
      Name: !Sub 'nonProdHostedZoneName'

【问题讨论】:

  • 我最终删除了我声明hostedZoneName=''的行

标签: amazon-web-services aws-lambda amazon-cloudformation aws-cloudformation-custom-resource


【解决方案1】:

您正在以nonProdHostedZoneName 的名义导出HostedZoneNameOutput

    Export: 
      Name: !Sub 'nonProdHostedZoneName'

但是你正在导入(未显示)名为HostedZoneName 的值。

基于 cmets 的更新(HostedZoneName 是列表,而不是字符串)。更正版本:

HostedZoneName: !ImportValue nonProdHostedZoneName

【讨论】:

  • 谢谢你的回答,我就是这样导入的。 HostedZoneName: - Fn::ImportValue: Fn::Sub: nonProdHostedZoneName
  • @eshmetchishik 您好,导入和导出时不需要`Fn::Sub:`。您发布的所有模板都没有HostedZoneName,如错误消息中所示。它来自哪里?
  • 它来自 myDNSRecord2:类型:AWS::Route53::RecordSet 属性:HostedZoneName:example.com。名称:mysite.example.com。类型:A TTL:'900' 资源记录:- 192.168.0.1 - 192.168.0.2 docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…
  • @eshmetchishik 我明白了。您使用HostedZoneName 的列表(注意-),而不是字符串。我更新了答案。
猜你喜欢
  • 1970-01-01
  • 2021-05-11
  • 1970-01-01
  • 2018-11-11
  • 2019-08-02
  • 2019-05-13
  • 2017-07-23
  • 2018-03-25
  • 2018-03-10
相关资源
最近更新 更多