3 Antworten
- Neueste
- Die meisten Stimmen
- Die meisten Kommentare
0
Hi, here you go..
Note: To allow run the 'aws s3 sync' command from the UserData section, you will also need to
- Create an IAM policy to enable reading of objects from the S3 bucket
- Create a Role to enable the EC2 instance to access the s3 bucket
- Add the Role to the EC2 instance on Launch creation time
Steps on how to do this are available at the following Link: https://optimalbi.com/blog/2016/07/12/aws-tips-and-tricks-moving-files-from-s3-to-ec2-instance/
#!/bin/bash
yum update -y
yum install httpd -y
service httpd start
chkconfig httpd on
cat > /tmp/subscript.sh << EOF
# START
echo "Setting up NodeJS Environment"
curl https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
echo 'export NVM_DIR="/home/ec2-user/.nvm"' >> /home/ec2-usr/.bashrc
echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm' >> /home/ec2-user/.bashrc
# Dot source the files to ensure that variables are available within the current shell
. /home/ec2-user/.nvm/nvm.sh
. /home/ec2-user/.bashrc
# Install NVM, NPM, Node.JS & Grunt
nvm alias default v12.7.0
nvm install v12.7.0
nvm use v12.7.0
curl -o- -L https://yarnpkg.com/install.sh | bash
export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
aws s3 sync s3://reactsearch /var/www/
cd /var/www/
yarn install
yarn run build
EOF
chown ec2-user:ec2-user /tmp/subscript.sh && chmod a+x /tmp/subscript.sh
sleep 1; su - ec2-user -c "/tmp/subscript.sh"
Hope that helps.
-randy
beantwortet vor 5 Jahren
0
HI rtakeshi, thank you for your reply and this working fine. now but could help me on if i want to write like a step in CNF.
configure_cfn:
files:
/tmp/subscript.sh:
content: !Sub |
echo "Setting up NodeJS Environment"
curl https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
echo 'export NVM_DIR="/home/ec2-user/.nvm"' >> /home/ec2-usr/.bashrc
echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm' >> /home/ec2-user/.bashrc
# Dot source the files to ensure that variables are available within the current shell
. /home/ec2-user/.nvm/nvm.sh
. /home/ec2-user/.bashrc
# Install NVM, NPM, Node.JS & Grunt
nvm alias default v12.7.0
nvm install v12.7.0
nvm use v12.7.0
curl -o- -L https://yarnpkg.com/install.sh | bash
export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
mode: "777"
owner: root
group: root
install_node:
commands:
01_config:
cwd: "/tmp/"
test: "test ! -e subscript.sh"
command: sleep 1; ./subscript.sh
HERE THIS SCRIPT IS NOT WORKING.
Please can guide where i am doing wrong here.
beantwortet vor 5 Jahren
0
Hi,
This appears to work in my environment:
---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'A simple template intended to be run in N.Virginia (us-east-1) with a Linux (1) distro. Feel free to change the ami so that you can run this in a different Region'
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: must be the name of an existing EC2 KeyPair.
Resources:
SSHSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH access via port 22
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: 22
IpProtocol: tcp
ToPort: 22
- CidrIp: 0.0.0.0/0
FromPort: 80
IpProtocol: tcp
ToPort: 80
EC2:
Type: AWS::EC2::Instance
Metadata:
AWS::CloudFormation::Init:
config:
commands:
a_download:
command: "wget https://reactsearch-scripts.s3.amazonaws.com/myscript.sh"
cwd: "/home/ec2-user"
b_setup:
command: "chown ec2-user:ec2-user myscript.sh && chmod a+x myscript.sh"
cwd: "/home/ec2-user"
c_run:
command: "su - ec2-user -c /home/ec2-user/myscript.sh"
cwd: "/home/ec2-user"
d_run_sync:
command: "aws s3 sync s3://reactsearch /var/www/"
e_yarn_install:
command: "su - ec2-user -c yarn install"
cwd: "/var/www"
f_yarn_run:
command: "su - ec2-user -c \"yarn run build\""
cwd: "/var/www"
Properties:
ImageId: ami-0b898040803850657
InstanceType: t2.micro
KeyName: !Ref KeyName
SecurityGroups:
- !Ref SSHSecurityGroup
UserData:
Fn::Base64:
!Join [ "", [
"#!/bin/bash -xe\n",
"sudo yum update -y\n",
"sudo yum install httpd -y\n",
"sudo service httpd start\n",
"sudo chkconfig httpd on\n",
"sudo yum install -y aws-cfn-bootstrap\n", #download aws helper scripts
"sudo /opt/aws/bin/cfn-init -v ", #use cfn-init to install packages in cloudformation init
!Sub "--stack ${AWS::StackName} ",
"--resource EC2 ",
!Sub "--region ${AWS::Region} ",
"\n" ] ]
IamInstanceProfile:
!Ref RootInstanceProfile
RootRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
RolePolicies:
Type: AWS::IAM::Policy
Properties:
PolicyName: root
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- "s3:ListAllMyBuckets"
- "s3:GetObjects"
- "s3:ListBucket"
Resource:
- "arn:aws:s3:::reactsearch-scripts"
- "arn:aws:s3:::reactsearch-scripts/*"
Roles:
- !Ref RootRole
RootInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: "/"
Roles:
- !Ref RootRole
I am storing the main myscript.sh script in an s3 bucket called, for example, "reactsearch-scripts"
myscript.sh:
#!/bin/bash
echo "Setting up NodeJS Environment"
curl https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
echo 'export NVM_DIR="/home/ec2-user/.nvm"' >> /home/ec2-user/.bashrc
echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm' >> /home/ec2-user/.bashrc
# Dot source the files to ensure that variables are available within the current shell
. /home/ec2-user/.nvm/nvm.sh
. /home/ec2-user/.bashrc
# Install NVM, NPM, Node.JS & Grunt
nvm alias default v12.7.0
nvm install v12.7.0
nvm use v12.7.0
curl -o- -L https://yarnpkg.com/install.sh | bash
export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
Hope this helps...
-randy
beantwortet vor 5 Jahren
Relevanter Inhalt
- AWS OFFICIALAktualisiert vor 8 Monaten
- AWS OFFICIALAktualisiert vor 8 Monaten
- AWS OFFICIALAktualisiert vor einem Jahr
- AWS OFFICIALAktualisiert vor 10 Monaten