bindo
bindo

Reputation: 87

Cloudformation refer resources on local files

I've just started with CloudFormation. My goal is to keep the template files shorter and cleaner. Is there a way in CloudFormation for the main template to refer to resources from locally stored files? For example, let's say I have a template for creating an EC2 resource and its corresponding IAM role. If I want to separate the IAM role from the main template and keep it in a different file, such as "iam-role.yml," can the main template retrieve the values from "iam-role.yml"? The closest option I could find in the documentation was "Transform: AWS::Include," but it doesn't seem to support local files but files kept in s3.

Here is a sample code with said 2 resources.

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t2.micro
      ImageId: ami-xxxxxxxxxxxxxxxxx 
      SecurityGroupIds:
        - sg-xxxxxxxxxxxxxxxxx
      SubnetId: subnet-xxxxxxxxxxxxxxxxx 
      KeyName: YourKeyName
      IamInstanceProfile: !Ref EC2Role

  EC2Role:  #this I want to keep in a diffrent file
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: ec2.amazonaws.com
            Action: sts:AssumeRole
      Policies:
        - PolicyName: EC2RolePolicy
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - ec2:DescribeInstances
                  - ec2:DescribeTags
                Resource: "*"

Cant I simply move the ECSRole in to iam-role.yml and make the EC2Instance to ref the EC2Role from that file? Appreciate your inputs, thanks.

Upvotes: 0

Views: 74

Answers (0)

Related Questions