Cloud Formation Mappings

Adding in Mappings, Parameters, UserData and an Outputs section.

Parameters:
  ServiceName:
    Description: "Stack Name"
    Type: String
  InstanceTypePara:
    Type: String
    Default: t2.micro
    AllowedValues:
      - t2.micro
      - m1.small
      - m1.large
    Description:
      EC2 instances
  SSHkey:
    Description: AJS ssh key
    Type: AWS::EC2::KeyPair::KeyName

Mappings:
  RegionMap:
    eu-west-1:
      AMI: ami-07d9160fa81ccffb5 # Amazon Linux AMI in Ireland
      TEXT: Test Text from Mapping
    us-east-1:
      AMI: ami-655a0a20 # Us East 1

Resources:
  Ec2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType:
        Ref:
          InstanceTypePara
      ImageId: !FindInMap
        - RegionMap
        - !Ref 'AWS::Region'
        - AMI
        # ami-07d9160fa81ccffb5 # Amazon Linux AMI in Ireland
      KeyName: !Ref SSHkey
      Tags:
        - Key: Name
          Value: !Ref ServiceName
        - Key: email
          Value: myname@company.co.uk
        - Key: BuiltBy
          Value: CloudFormation
        - Key: JoinTest
          Value: !Join [ ":", [ Join, Example, in, YAML ] ]
        - Key: BuiltInRegion
          Value: !Ref AWS::Region
        - Key: MappingTest
          Value: !FindInMap
            - RegionMap
            - !Ref 'AWS::Region'
            - TEXT
      SecurityGroups:
        - !Ref SecurityGroupSSH
      UserData:
        !Base64 |
        #!/bin/bash -xe
        yum update -y
        yum install httpd -y
        service httpd start

  SecurityGroupSSH:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Allow SSH
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: '22'
          ToPort: '22'
          CidrIp: 0.0.0.0/0
      Tags:
        - Key: Name
          Value: AJS - Allow SSH
  SecurityGroupHttp:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Allow http
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: '80'
          ToPort: '80'
          CidrIp: 0.0.0.0/0
      Tags:
        - Key: Name
          Value: AJS - Allow http

  S3BucketExample:
    Type: AWS::S3::Bucket
    Properties:
      AccessControl: PublicRead
      BucketName: ajs-bucket
      WebsiteConfiguration:
        IndexDocument: index.html
      Tags:
        - Key: Name
          Value: AJSTestBucket
        - Key: builtby
          Value: CloudFormation

Outputs:
  InstanceDNS:
    Description:
      DNS name of instance
    Value:
      !GetAtt
      - Ec2Instance
      - PublicDnsName
  WebsiteURL:
    Description:
      Web site URL
    Value:
      !Sub
       - 'http://${EC2Instance.PublicDnsName}'
  BucketURL:
    Description: BucketURL
    Value:
      !GetAtt
      - S3BucketExample
      - WebsiteURL
  BucketARN:
    Description: BucketARN
    Value:
      !GetAtt
      - S3BucketExample
      - Arn
 
aws/aws-cloudformation_mappings_outputs.txt · Last modified: 15/02/2024 10:20 by andrew