Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Feature/aws ami docker support #11

Merged
merged 13 commits into from
Nov 22, 2017
Merged

Feature/aws ami docker support #11

merged 13 commits into from
Nov 22, 2017

Conversation

MatthiasScholz
Copy link
Contributor

@MatthiasScholz MatthiasScholz commented Oct 7, 2017

EXTENSION: AWS AMI Creation with Docker support

The current solution only allows to create AMIs with nomad and consul preinstalled, docker support is missing. This PR adds this for the already supported Amazon Linux and Ubuntu16 AMIs.

  • NEW: Putting Docker installation routine into the packer script.

  • REFACTOR: Image setup routine to make the packer script more readable
    -> Extracted setup commands into separate scripts.

  • TEST: Amazon Linux and Ubuntu16 with Docker support were verified.

To keep the packer template clear the installation of nomad and the
consul was moved into a separate shell script.
Added support for Docker for the Amazon Linux and Ubuntu16 AMI creation in
conjunction with the usage of the AWS ECR ( private repository ).
WARNING: Using AWS credentials inside the AMI.
Copy link
Collaborator

@brikis98 brikis98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I assume your goal here is to show how to create Nomad client nodes that have Docker installed so Nomad can run Docker containers on them? If so, I think that's a great idea, but please update the appropriate READMEs with this information!

Amazon Linux with Docker support was verified, Ubuntu16 not.

Please test both. Installing Docker on Ubuntu can be a bit quirky :)

@@ -0,0 +1,3 @@
[default]
aws_access_key_id = TODO_ADD_YOUR_KEY_HERE
aws_secret_access_key = TODO_ADD_YOUR_SECRET_HERE
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is just example code, but I don't want to encourage users storing their secrets in plain text and copying them to a server. It's not a good pattern to follow. A better pattern to follow is to create an IAM role with ECR permissions and to attach that IAM role to the server (I believe the ECR cred helper supports this just fine).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed this is not the recommended production safe way. This approach was chosen for simplicity.

To adapt this to more secure way this will take some more time to update the approach and an increased configuration effort for the user. For testing nomad it might be acceptable. But nevertheless this security hole should be mentioned in the documentation.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To adapt this to more secure way this will take some more time to update the approach and an increased configuration effort for the user

You can make it part of the example code directly! You just attach ECR permissions to the IAM role of the Nomad client servers. It's actually much easier for the user this way, and there is no security hole.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding this module to do the trick?
https://github.com/cloudposse/terraform-aws-ecr

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting find. It seems to do a bit more than we need though. All we really want is an IAM role to read from ECR, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends on the scope and the predefined environment. Since this terraform module does not provide an AWS ECR preconfiguration the user is in charge to provide or create one. When you start from scratch it could be convenient to include the batteries, but it will also enlarge the example.

Maybe it is the best to remove the AWS ECR support from this PR and create a second PR. Most important at the moment is the docker support.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, handling ECR in a separate PR makes sense.

},
{
"ami_name": "nomad-consul-amazon-linux-{{isotime | clean_ami_name}}",
"ami_description": "An Amazon Linux AMI that has Nomad and Consul installed.",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The names and descriptions of both of these should be updated to indicate Docker is installed too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
"type": "file",
"source": "setup_ubuntu16.sh",
"destination": "/tmp/",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use the file provisioner to copy this script rather using the script provisioner to execute it directly? Same goes for the same pattern below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way was chosen since the content of the script section for packer became hardly readable. Since the script has to be execute on the machine it needs to be copied before.

Is there a more easy way to do this directly in the script section directly?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It both uploads a script file and executes it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"only": [
"amazon-linux-ami"
]
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per my comment above, this should be done using an IAM role at run time rather than hard-coded, plaintext credentials at build time.

"type": "shell",
"environment_vars": [ "NOMAD_VERSION={{user `nomad_version`}}",
"CONSUL_VERSION={{user `consul_version`}}",
"CONSUL_MODULE_VERSION={{user `consul_module_version`}}"],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON formatting puts the square brackets on their own lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


# NOTE: git is already preinstalled
#echo "[INFO] [${SCRIPT}] Setup git"
#sudo apt install -y git
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was kept for convenience since it is a real dependency it should be made visible.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. Could you update the comment to say "Note: git is required, but it should already be preinstalled on Ubuntu 16.0"

@MatthiasScholz
Copy link
Contributor Author

MatthiasScholz commented Oct 10, 2017

Updated initial PR request TEST comment after verification. Ubuntu16 is working as well.

@MatthiasScholz
Copy link
Contributor Author

Sorry for the long delay in providing updates to this PR. Sadly at the moment I am to busy to work on this. I will have a look at it again in the middle of November.

AWS ECR support was removed because it is a securtiy flaw to add the
AWS credentials into the AMI directly. This feature will be
reintergrated later using AWS IAM roles instead.
@MatthiasScholz
Copy link
Contributor Author

MatthiasScholz commented Nov 19, 2017

UPDATED:

  • AWS ECR support removed
  • Explanation regarding preinstalled git on Ubuntu16 added.

Copy link
Collaborator

@brikis98 brikis98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thanks!

Just a final tweak around how scripts are copied/executed.

},
{
"type": "file",
"source": "setup_nomad_consul.sh",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why in this Packer template are you copying the script to the server and running it from /tmp, whereas in the above you are just executing it directly using the script keyword? We should stick with one approach for consistency and it seems like the latter is simpler.

"provisioners": [
{
"type": "shell",
"script": "setup_ubuntu16.sh",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use {{template_dir}} so the paths work, even if you're running Packer from a different folder.

},
{
"type": "shell",
"script": "setup_amazon-linux.sh",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use {{template_dir}} so the paths work, even if you're running Packer from a different folder.

"CONSUL_VERSION={{user `consul_version`}}",
"CONSUL_MODULE_VERSION={{user `consul_module_version`}}"
],
"script": "setup_nomad_consul.sh"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use {{template_dir}} so the paths work, even if you're running Packer from a different folder.

Copy link
Collaborator

@brikis98 brikis98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful, thank you!

@brikis98 brikis98 merged commit 78e9478 into hashicorp:master Nov 22, 2017
@brikis98
Copy link
Collaborator

@MatthiasScholz MatthiasScholz deleted the feature/aws-ami-docker-support branch November 24, 2017 20:32
Tethik pushed a commit to Tethik/terraform-aws-nomad that referenced this pull request Feb 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants