CloudFormation Resource Creation if not exist
Asked Answered
T

3

21

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" ] }
    ]
  }
}
Trousers answered 5/3, 2019 at 7:49 Comment(0)
R
8

This is not exactly the answer you need. But in general, you can use Conditions for this. In your 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.

Rhodonite answered 5/3, 2019 at 11:52 Comment(3)
in my case probably i will get parameter about resource creation from user .Trousers
@Trousers The conditional doesn't have to be just about a passed parameter. Did you ever get it all worked out?Familiar
also see intrinsic-function-reference-conditionsUnearned
K
2

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:

Kickstand answered 5/3, 2019 at 14:44 Comment(3)
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.Kalynkam
@Kalynkam 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.Lazaretto
This is the same, sad, conclusion I came to. I hate CDK/CF.Ern
H
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]
Hecto answered 21/12, 2021 at 10:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.