0

I did all the changes to AWS EC2 machine for Nginx & settings up PHP values and tested everything and than baked the AMI so it can be launched under AWS auto-scaling group.

All is good, but when developers or application rewrite requires sometimes to make change in PHP values or doing some new rewriting rules in nginx conf file, I always have to re-bake the whole AMI after each time disturbing my complete auto-scaling infrastructure and making server down.

I must be wrong! what is the easiest/best way of doing these kind of changes without affecting existing auto-scale group. May be symlinks to NFS share for php.ini and nginx conf files? if the solution is only symlinks than how it should be done?

Farmi
  • 379
  • 1
  • 4
  • 17

1 Answers1

0

Have a look at EC2 User Data. You could set up something that downloads a script from S3 and runs it.

When you launch an instance in Amazon EC2, you have the option of passing user data to the instance that can be used to perform common automated configuration tasks and even run scripts after the instance starts. You can pass two types of user data to Amazon EC2: shell scripts and cloud-init directives. You can also pass this data into the launch wizard as plain text, as a file (this is useful for launching instances via the command line tools), or as base64-encoded text (for API calls).

If you are interested in more complex automation scenarios, consider using AWS CloudFormation and AWS OpsWorks. For more information, see the AWS CloudFormation User Guide and the AWS OpsWorks User Guide.

I've tried this out myself, it was really easy - it took me about 20 minutes including creating a few instances for testing and writing it up.

  1. Go into IAM and create a new IAM role, or if you already have a role assigned to the EC2 instances edit the current role. Make sure the role allows read only access to S3 - just follow the wizard through. You could use a custom role to allow access to a specific bucket if you like.

  2. Create a new launch configuration that does two things:

    • Specifies this IAM role
    • Under advance, have a script in the user data. The script below copies any files and subfolders from an s3 folder to a folder on your new instance. I've provided the script as an image because SF formatting isn't working right. The text version with a few characters missing is below it.

EC2 S3 bootstrapping

/bin/bash  - NB missing #1 at start of line
aws s3 cp  s3://s3-bucketname/foldername/ /ec2-target-folder --recursive

You'll have to check into users and permissions yourself as they're different for each setup.

Tim
  • 31,888
  • 7
  • 52
  • 78
  • I am sure that there must be some easy way of doing this.... – Farmi Dec 22 '16 at 15:12
  • 1
    This probably is the easy way. Basically, you can run a script on startup, that script copies files from S3 for your Nginx configuration. It's probably about 5 lines, but I haven't done it before so I can't supply those lines. If I get time today I might experiment a little, seems like something I might like to know. – Tim Dec 22 '16 at 16:41
  • @Farmi I've tried this out and posted a procedure for you. It's really easy. If you're not familiar with AWS it might take a bit longer just to work out what's going on, but I could probably do screen shots if you get really stuck. – Tim Dec 22 '16 at 18:56