Here I describe how we could use AWS Code deploy to transfer your code to an EC2 server for deployment. AWS code deploy can be used for much more, but that is not the topic of this blog.
If you have a single server (and not a cluster) you could do a much simpler deployment by using SSH and shell scripts.
For AWS Code deploy to transfer the code to your production or staging server. You need to do the following
aws ec2 associate-iam-instance-profile --instance-id i-xyzabcdef --iam-instance-profile Name=Myapp-Instance-Profile
aws ec2 describe-iam-instance-profile-associations
The above is described in more detail in this link
wget https://aws-codedeploy-ap-south-1.s3.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
Now check status:
sudo service codedeploy-agent status
The above steps are further described in this link.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:ListAllMyBuckets","s3:PutObject"],
"Resource": "arn:aws:s3:::*"
},
{
"Effect": "Allow",
"Action": ["codedeploy:*"],"Resource": "*"
}
]
}
On your code structure, you would need to create an appspec.yml file which could contain commands which you need to execute on deployment.
An appspec yaml file is as simple as shown below, this tells AWS code deploy to run the installCode.sh
shell script for Application Start. Like the ApplicationStart
tag below, there are other life cycle hooks available.
version: 0.0
os: linux
files:
- source: /
destination: /home/ec2-user/build
hooks:
ApplicationStart:
- location: installCode.sh
timeout: 300
runas: root
A Sample appspec.yml file, can be found in this link
Thats about it, for the setup. To push code to your EC2 instance you would have to run the below commands. [Assuming you have the aws cli and keys installed on your system]
$ aws deploy push --application-name <application-name> --s3-location s3://<bucket name>/<any build name> --source <directory>
$ aws deploy create-deployment --application-name <application-name> --s3-location bucket=<bucket-name>,key=<any build name>,bundleType=zip,eTag=<tag from deploy command> --deployment-group-name <deployment group name>
Hope this was useful. You could write to me at jai@tech47.in