问题描述
我为 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 (将#修改为@)