0

I used packer to upload a docker image I created to my AWS ECR repository.

How can I use the AWS cli to automatically locate that image, create a new cluster, set the port rules to allow all inbound and outbound traffic and then spin up the container? I want to automate this process so I can integrate it into packer later, as a post processor.

Can someone explain me how I can do this?

{
    "type": "docker-push",
    "ecr_login": true,
    "aws_access_key": "<snip>",
    "aws_secret_key": "<snipe>",
    "login_server": "https://<snip>.dkr.ecr.eu-west-1.amazonaws.com/"
}

Currently the last part of my packer script just uploads the image - now I need a way to automatically start this docker image and make sure the network traffic settings are configured correctly.

2
  • Could you indicate whether you solved the issue?
    – 030
    Dec 23, 2019 at 8:26
  • @030 The issue not resolved but I think I should create a new thread with a more descriptive explanation of the problem.
    – Kyu96
    Dec 25, 2019 at 15:32

2 Answers 2

2

The answer depends on how you want to run the container. There are 3 ways you could accomplish this:

  1. Since you mentioned creating a cluster, you could create a new Kubernetes cluster and run your containers in this cluster. AWS does support creating a cluster and managing of it via the CLI.
  2. You can choose to run the containers as part of AWS Elastic Container Service (ECS). This supports launching the containers via AWS Fargate (essentially a "serverless" type approach where you pay for CPU and Memory consumed) or on a standard EC2 instance. ECS has its own CLI (separate install). ECS will also allow you to set the ports and set inbound/outbound rules.
  3. Finally, you can choose to use the CLI to spin up an EC2 instance and then manually or via a script start the docker containers. However, you'd be much better off choosing one of the first two options.

Finally, AWS does have a tutorial that seems to go over exactly what you are trying to accomplish (configuring a container to run via the CLI).

3
  • I assume the second option is best suited. For now, I'll overlook the "create a cluster" part, and just go with the default cluster. So if I understand that correctly I now need to specify a task definition that uses my image that is located on ECR and needs to pull that image and then somehow start that. That's already where I am starting to struggle. I need to specify a Secrets Manager ARN or name - and I do not know the path. Since I am using an educate account my secrets are stored on rosettahub.com. How can I find the correct ARN so the task can pull my image from the ECR
    – Kyu96
    Dec 20, 2019 at 16:46
  • You shouldn't need anything beyond the proper IAM permissions and the task definition to get things going. Check out step 5 of the AWS tutorial I linked to for an example of deploying a busybox container via the CLI. As for how to pull from ECR, I think you can find that information from the ECR GUI within AWS. Dec 20, 2019 at 17:02
  • I am still having a hard time to get the big picture of everything, even after reading the guide you linked. My docker image is on the ECR. ECS is used to run docker images on a cluster, right? What steps are needed in what order to get my image that is laying in the ECR to run on ECS? I somehow need to tell ECS to pull the image from the ECR (which requires certain permissions?) ... But before that I already need a running instance on ECS where the pull command is executed on? And I also need to start it somehow ..? I just fail to understand in what order I need to do things.
    – Kyu96
    Dec 21, 2019 at 13:43
1

In my opinion, Packer should only be used for building something like a VM or docker image. If you would like to run a VM then you could subsequently use Vagrant or docker-compose in case of docker.

now I need a way to automatically start this docker image and make sure the network traffic settings are configured correctly

If one is running k8s then one could update the tag of a docker-image that is defined in the deployment.yaml and do a kubectl up again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.