6

I am trying to create a custom AMI using packers. I want to install some specific software on the custom AMI and my setups are present in S3 bucket. But it seems there is no direct way to download S3 file in packers just like cfn-init.

So is there any way to download file on EC2 instance using packers.

Praveen Raghav
  • 163
  • 1
  • 2
  • 7
  • You should be able to download any file into your Packer AMI. If you're running a Linux-based OS, just use `wget`; if you're on Windows, use Powershell's `(New-Object System.Net.WebClient).DownloadFile` command. – Adil B Apr 17 '18 at 19:45
  • 1
    That would assume the bucket is open – BobMonk Feb 22 '21 at 22:56

3 Answers3

7

Install the awscli in the instance and use iam_instance_profile to give the instance permissions to get the files from S3.

Rickard von Essen
  • 4,110
  • 2
  • 23
  • 27
1

I can envisage an instance where this is ineffective.

When building the image upon aws you use your local creds. Whilst the image is building this building packer image has a packer user and is not you and so not your creds and can't access the S3 (if private)

One option https://github.com/enmand/packer-provisioner-s3

Two option, use local-shell provisioner you pull down the S3 files to your machine using aws S3 cp, then file provisioner to upload to the correct folder in the builder image, you can then use remote-shell to do any other work on the files. I chose this as, although it's more code, it is more universal when I share my build, other have no need to install other stuff

Three option wait and wait. There is an enhancement spoke of in 2019 packer GitHub to offer an S3 passthrough using local cars but isn't on the official roadmap.

BobMonk
  • 178
  • 1
  • 10
0

Assuming awscli is already installed on Ec2, use below sample commmand in a shell provisioner.

sudo aws s3 cp s3://bucket-name/path_to_folder/file_name /home/ec2-user/temp

VimalKumar
  • 1,611
  • 1
  • 14
  • 11