如何构建cloudformation模板以安装mulesoft和jre软件包?

时间:2019-04-18 04:50:26

标签: amazon-cloudformation

我有一个可启动ec2实例的cloudformation模板,但我也想安装软件包。不是那样做的。

在实例上手动运行时,模板中的命令成功安装了软件包。但是我不了解正确的语法才能让cloudformation为我安装。

AWSTemplateFormatVersion: '2010-09-09'
Description: AWS CloudFormation Sample Template - spin up EC2 instance, install mule and jre
Parameters:
  KeyName:
    Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
    Default: app-key
    Type: AWS::EC2::KeyPair::KeyName
    ConstraintDescription: must be the name of an existing EC2 KeyPair.
  InstanceType:
    Description: MuleSoft Enterprise Standalone EC2 instance
    Type: String
    Default: t2.small
    AllowedValues:
      - t2.small
      - z1d.large
      - r5d.large
      - r5.large
      - r5ad.large
      - r5a.large
    ConstraintDescription: must be a valid EC2 instance type.
  SSHLocation:
    Description: The IP address range that can be used to SSH to the EC2 instances
    Type: String
    MinLength: '9'
    MaxLength: '18'
    Default: 0.0.0.0/0
    AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})"
    ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.

Resources:
  EC2Instance:
    Type: AWS::EC2::Instance
    Metadata:
      AWS::CloudFormation::Init:
        config:
          commands:
            01_update_yum:
              command: "sudo yum update -y"
            02_rm_jre1_7:
              command: "sudo sudo yum -y erase java-1.7.0"
            03_install__jre1_8:
              command: "sudo yum install -y java-1.8.0-openjdk"
            04_change_into_opt:
              command: "cd /opt"
            05_download_mulesoft:
              command: "sudo wget https://s3-us-west-1.amazonaws.com/mulesoft/mule-enterprise-standalone-4.1.3.2.zip"
            06_install_mulesoft:
              command: "sudo unzip mule-enterprise-standalone-4.1.3.2.zip"
            07_add_mule_user:
              command: "sudo useradd mule"
            08_mule_ownership:
              command: "sudo chown -R mule /opt/mule-enterprise-standalone-4.1.3.2"
            09_run_mule:
              command: "sudo -u mule bash -x /opt/mule-enterprise-standalone-4.1.3.2/bin/mule console"
    Properties:
      InstanceType:
        Ref: InstanceType
      SecurityGroups:
      - Ref: WebSecurityGroup
      KeyName:
        Ref: KeyName
      ImageId: ami-0080e4c5bc078760e

  WebSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Enable SSH, HTTP, HTTPS, Custom port
      SecurityGroupIngress:
        - CidrIp: 0.0.0.0/0
          FromPort: '80'
          IpProtocol: tcp
          ToPort: '80'
        - CidrIp: 0.0.0.0/0
          FromPort: '443'
          IpProtocol: tcp
          ToPort: '443'
        - CidrIp: 0.0.0.0/0
          FromPort: '8443'
          IpProtocol: tcp
          ToPort: '8443'
        - CidrIp:
            Ref: SSHLocation
          FromPort: '22'
          IpProtocol: tcp
          ToPort: '22'

Outputs:
  InstanceId:
    Description: InstanceId of the newly created EC2 instance
    Value:
      Ref: EC2Instance
  AZ:
    Description: Availability Zone of the newly created EC2 instance
    Value:
      Fn::GetAtt:
      - EC2Instance
      - AvailabilityZone
  PublicDNS:
    Description: Public DNSName of the newly created EC2 instance
    Value:
      Fn::GetAtt:
      - EC2Instance
      - PublicDnsName
  PublicIP:
    Description: Public IP address of the newly created EC2 instance
    Value:
      Fn::GetAtt:
      - EC2Instance
      - PublicIp

我希望此模板启动实例并执行命令以安装软件包。

1 个答案:

答案 0 :(得分:0)

您应同时使用 Userdata cloud-init 进行安装。您有命令,但未调用这些命令。
试试这个https://www.bogotobogo.com/DevOps/AWS/aws-CloudFormation-Bootstrap-UserData.php。此页面提供了安装软件包的正确示例。