How to upload a file to all EC2 instances without using SCP/SSH?

Hi! I have an application running on two EC2 instances and I would like to automate config change deployments for it. The process is very simple, I just need to build a config file in GitLab CI, upload it to a particular path within my EC2 instances, and then run a command to restart my application so that the new config is picked up.

What would be the proper way of copying my config file to all EC2 instances? I want to avoid manually SCPing to them, as that would require hardcoding the instance URLs. I imagine the right tool is SSM, but I haven’t found a way to do this with it.


Related discussions:

If the problem is you don’t want to do it manually, I’d suggest Ansible with EC2 dynamic inventory.

Bake a new AMI and replace it behind the ASG.

Yep this is the answer!

Upload the file to S3 as part of your CI process and download it before restarting in your app restart process?

What question?

CodeDeploy could do what you’re after..

You could make instances poll your git repo for changes or configure codebuild to push changes to running instances when there are changes to config file

GitLab can deliver the config changes to an S3 bucket. CodeBuild can trigger on the S3 event and hand off the file to CodeDeploy.

It’s what CodePipeline is made for.

I usually hook up CodeBuild to GitHub, which will trigger a new installation on commit, BUT there are many ways to get the same result.

SSM would be my choice - if all the machines are in the same group, and you’re running AMZ linux, and the boxes have s3 access via IAM, it’s a simple run command to have them all pull down a file https://docs.aws.amazon.com/systems-manager/latest/userguide/walkthrough-cli.html

Create an SSM Document that downloads from S3 and restarts the app. Then setup a CloudWatch Event on your S3 bucket to execute the document on your instances as targets by tag name (in order to do more than one instance). Link: https://docs.aws.amazon.com/systems-manager/latest/userguide/rc-cwe.html

This. At my company we completely banned SSH and use SSM instead.

That’s the route I was going, although I’m thinking of having the instances pull the artifact down from gitlab directly

Thanks, I haven’t looked into these but it sounds like a good fit

If what worries you is changing the url, you can use elastic ips

That said, I like the idea of uploading to S3. In fact, that’s exactly what I do.

or Chef, or Salt, or Puppet, or Puppet Bolt, or any of the actual config management tools, or if you wanted to get creative watchtower, which watches for changes to git repos. You could even use terraform and null_resource. it all depends which your happier with using and approach. Ansible, bolt or terraform will work really well from available gitlab ci and containers.

Alternatively, you could have the application pull it from a git repo. Same exact idea as using S3, but if it is already in git, it would be one less step (uploading to S3). If it is not in a git repo or there is pain in setting up the instance to pull, S3 is my suggestion as well.

SSM is supposed to be used for patch stuff, but could in principle be uded too, if you bend it a bit.

I’ve just recently gotten more into SSM and it alows you to connect to instances in private subnets without the need for a bastion/jump host OR the need for an ssh key. Really handy if you are working for a number of different customers with different workstation security rules.

Don’t discard SSM, but maybe don’t make it your priority right now :slight_smile:

In a perfect world, you don’t need to connect to your machines at all.. CodeDeploy will do anything that you would want to do on the machines just fine.

Indeed. This would be the best way to manage (and version) a config file, I assumed (wrongly, maybe) there wasn’t any git pull involved on the nodes.

This assumes your repo is public. It also assumes dev tools are installed in production (generally a bad operations practice).

Downloading from S3 through a role assigned to the host is the answer or bake the config into the host.

SSH supports associating commands to a key instead of giving it a shell. The process could be

  1. copy to S3
  2. SSH to host with command key which downloads config, validates config, updates app with new config.

I’ve done this in high security environments where one SSH key can upload a file into a chroot jail (SFTP) and another runs a script.