微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AWS Cloudformation 为 ec2 用户数据调用 bash 脚本

如何解决AWS Cloudformation 为 ec2 用户数据调用 bash 脚本

我为 EC2 实例创建了一个 cloudformation 模板,它将在用户数据中设置 Tomcat。当脚本嵌入到模板中时,模板工作正常。但我想在单独的 bash 脚本(名为 tomcat.sh)中移动脚本,并使用 userdata 作为来自 AWS CLI 的 cfn 参数调用脚本。但得到错误

aws cloudformation create-stack --stack-name EC2Instance2 --template-url s3://cfn04012021/14-01-CFN-EC2-Instance2.yml --parameters ParameterKey=UserData,ParameterValue=$(base64 -w0 tomcat)

usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text,you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help

UnkNown options: -w0,tomcat)

cloudformation模板如下

AWstemplateFormatVersion: 2010-09-09
Description: EC2 Instance with Instance profile role to access S3 buckets

Parameters:
  KeyName:
    Description: Key name to SSH to VM's. 
    Type: AWS::EC2::KeyPair::KeyName
  UserData:
    Type: String
    Default: ""

  MyStageVMInstance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-0533f2ba8a1995cf9
      InstanceType: t2.micro
      KeyName: !Ref KeyName
      SecurityGroups:
        - !Ref MySecurityGroup
      IamInstanceProfile: !Ref MyInstanceProfile
      Tags:
        - Key: Name        
          Value: dev
      UserData: !Ref UserData

bash 脚本是

#!/bin/bash|
sudo amazon-linux-extras install java-openjdk11
sudo groupadd --system tomcat 
sudo useradd -d /usr/share/tomcat -r -s /bin/false -g tomcat tomcat
getent passwd tomcat
getent group tomcat
export VER="9.0.41"
wget https://archive.apache.org/dist/tomcat/tomcat-9/v${VER}/bin/apache-tomcat-${VER}.tar.gz
sudo tar xvf apache-tomcat-${VER}.tar.gz -C /usr/share/
sudo ln -s /usr/share/apache-tomcat-$VER/ /usr/share/tomcat
sudo chown -R tomcat:tomcat /usr/share/tomcat
sudo chown -R tomcat:tomcat /usr/share/apache-tomcat-$VER/ 
sudo tee /etc/systemd/system/tomcat.service<<EOF
[Unit]
Description=Tomcat Server
After=syslog.target network.target

[Service]
Type=forking
User=tomcat
Group=tomcat

Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment='JAVA_OPTS=-Djava.awt.headless=true'
Environment=CATALINA_HOME=/usr/share/tomcat
Environment=CATALINA_BASE=/usr/share/tomcat
Environment=CATALINA_PID=/usr/share/tomcat/temp/tomcat.pid
Environment='CATALINA_OPTS=-xms512M -Xmx1024M'
ExecStart=/usr/share/tomcat/bin/catalina.sh start
ExecStop=/usr/share/tomcat/bin/catalina.sh stop

[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl start tomcat
sudo systemctl enable tomcat

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。