Labels

Aws (32) MySQL (20) Aurora (12) Rds (7) DynamoDB (6) Cloudformation (4) Python3 (4) Unix (3) MongoDB (1)

Wednesday, 7 February 2018

Cloudformation and DependsOn attribute

With the DependsOn attribute you can specify that the creation of a specific resource follows another.

Important
Dependent stacks also have implicit dependencies. For example, if the properties of resource A use a !Ref to resource B, the following rule apply:
  • Resource B is created before resource A.
  • Resource A is deleted before resource B.
Example
AWSTemplateFormatVersion: '2010-09-09'
Mappings:
  RegionMap:
    us-east-1:
      AMI: ami-76f0061f
    us-west-1:
      AMI: ami-655a0a20
Resources:
  Ec2Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId:
    Fn::FindInMap:
    - RegionMap
    - Ref: AWS::Region
    - AMI
    DependsOn: myDB
  myDB:
    Type: AWS::RDS::DBInstance
    Properties:
      AllocatedStorage: '5'
      DBInstanceClass: db.m1.small
      Engine: MySQL
      EngineVersion: '5.5'
      MasterUsername: MyName
      MasterUserPassword: MyPassword

When a DependsOn attribute is required

  • VPC-gateway attachment
  • Amazon ECS Service and Auto Scaling Group
  • IAM Role Policy

Further documentation here