Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CloudFormation Resource Creation if not exist

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" ] }
    ]
  }
}
like image 617
ColossusMark1 Avatar asked Mar 05 '19 07:03

ColossusMark1


People also ask

What happens when one of the resources in a CloudFormation stack Cannot be created successfully?

Q: What happens when one of the resources in a stack cannot be created successfully? By default, the “automatic rollback on error” feature is enabled. This will direct CloudFormation to only create or update all resources in your stack if all individual operations succeed.

What happens when CloudFormation stack creation fails?

If stack creation fails, go to the CloudFormation Resources list in the AWS Management Console to find the log group. Note that if stack creation fails before any instances are launched, a log group might not be created. By default, AWS deletes CloudWatch log groups if stack creation fails.

What is FN :: if?

Fn::If. Returns one value if the specified condition evaluates to true and another value if the specified condition evaluates to false .


2 Answers

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.

like image 70
asr9 Avatar answered Oct 19 '22 20:10

asr9


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:

  • https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html
  • https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cfn-customresource.html
like image 1
Biplob Biswas Avatar answered Oct 19 '22 19:10

Biplob Biswas