16

I want to create Route53 HostedZone with CloudFormation so I want to check some information in Route53 about HostedZone is exist.

In logic of my case I need check if resource is exist, ignore the resource creation. How I can handle this problem.

My CloudFormation template show at below.

"myDNSRecord" : {
  "Type" : "AWS::Route53::RecordSet",
  "Properties" : {
    "HostedZoneName" : { "Ref" : "HostedZoneResource" },
    "Comment" : "DNS name for my instance.",  
    "Name" : {
      "Fn::Join" : [ "", [
        {"Ref" : "Ec2Instance"}, ".",
        {"Ref" : "AWS::Region"}, ".",
        {"Ref" : "HostedZone"} ,"."
      ] ]
    },
    "Type" : "A",
    "TTL" : "900",
    "ResourceRecords" : [
      { "Fn::GetAtt" : [ "Ec2Instance", "PublicIp" ] }
    ]
  }
}
Walery Strauch
  • 6,792
  • 8
  • 50
  • 57
ColossusMark1
  • 1,189
  • 4
  • 14
  • 27

3 Answers3

7

This is not exactly the answer you need. But in general, you can use Conditions for this. In you template, you define your condition in Conditions section and use it to conditionally create the resource. e.g.

Parameters:
  EnvironmentSize:
    Type: String
    Default: Micro
    AllowedValues:
      - Micro
      - Small
      - Medium
      - AuroraCluster
Conditions:
  isntAuroraCluster:
    !Not [!Equals [!Ref EnvironmentSize, "AuroraCluster"]]
DBInstance:
  Type: AWS::RDS::DBInstance
  Condition: isntAuroraCluster
  Properties:
    DBInstanceClass: !FindInMap [InstanceSize, !Ref EnvironmentSize, DB]
    <Rest of properties>

Here my RDS DBinstance is only created if my environment size is not AuroraCluster.

If you don't find a better solution, you could take that as user input (whether to create a record set or not) & use that as condition to create your resource. Hope it helps.

asr9
  • 2,440
  • 1
  • 21
  • 37
1

The best way to do this would be to do the following:

  1. Create a lambda backed custom resource
  2. Check using lambda whether your resource exists or not, depending on that return an identifier
  3. Use cloudformation conditions to check on the value of the returned identifier and then correspondingly create or not create the resource.

You can fetch the return value of the custom resource using !GetAtt

More information can be found on the AWS websites relating to custom resource:

Biplob Biswas
  • 1,761
  • 19
  • 33
  • Were you ever successful with this? As far as I can tell, you can't reference resources in the conditions block of the template like you're suggesting. – ScottieMc Dec 08 '20 at 00:37
  • @ScottieMc I don't think he is suggesting that at all, but I can be wrong. I have a similar need for something like this... I have an apigw2 template with apistage and I want the stage to always build, but only for a single api with a single name. – Sinux1 Nov 03 '21 at 22:42
0

You can try to orchestrate creation of specific resources using AWS::NoValue

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html

Below is taken from variables creation for LambdaFunction

Conditions:
   IsProd: !Equals [!Ref Env, "production"]

Environment:
   Variables:
     USER: !If [IsProd, !GetAtt ...., Ref: AWS::NoValue]
marcin2x4
  • 1,321
  • 2
  • 18
  • 44