0
votes

currently I try to simply spin up an ec2 instance to show myfile2.txt in the tmp folder currently the cloud formation launches. but no file. I have tried working around in the command line and check the logs but I don't see what the issue is I've been having trouble with AWS::CloudFormation::Init: for a while and was wondering if anyone had any suggestions??

This is a just simple exercise that is part of a greater project.

AWSTemplateFormatVersion: "2010-09-09"
Parameters:
  InstanceTypeParameter:
    Type: String
    Default: t2.micro
    AllowedValues:
      - t2.micro
      - m1.small
      - m1.large
    Description: Enter t2.micro, m1.small, or m1.large. Default is t2.micro.
  KeyName:
    Description: key pair name
    Type: AWS::EC2::KeyPair::KeyName

Resources:
  WebInstance:
    Type: AWS::EC2::Instance
    Metadata:
        AWS::CloudFormation::Init:
          config:
            files:
              '/tmp/myfile2.txt':
                content: "Hello sir"
                mode: '000755'
                owner: root
                group: root
    Properties:
        InstanceType:
          Ref: InstanceTypeParameter
        ImageId:  ami-04b762b4289fba92b
        SecurityGroupIds:
          - sg-0394c353eef246c71
        SubnetId: subnet-476a773e
        Tags:
          -
            Key: Name
            Value: Trovo-webserver
        KeyName:
          Ref: KeyName
        UserData:
          Fn::Base64:
            !Sub |
              #!/bin/bash -xe

1

1 Answers

0
votes

It's not clear if you're running cfn-init command or not. Ideally your userdata section would have a command cfn-init which in turn would invoke the configsets defined under Init key. Please check the sample snippet below.

UserData: !Base64 
  'Fn::Join':
    - ''
    - - |
        #!/bin/bash -xe
      - |
        # Install the files and packages from the metadata
      - '/opt/aws/bin/cfn-init -v '
      - '         --stack '
      - !Ref 'AWS::StackName'
      - '         --resource WebServerInstance '
      - '         --configsets InstallAndRun '
      - '         --region '
      - !Ref 'AWS::Region'
      - |+