{"id":25125736,"url":"https://github.com/p4irin/explore_terraform_awscli_ec2_localstack","last_synced_at":"2025-04-02T18:42:07.597Z","repository":{"id":276139743,"uuid":"928328792","full_name":"p4irin/explore_terraform_awscli_ec2_localstack","owner":"p4irin","description":"Create an AWS EC2 instance using AWS CLI and Terraform on Localstack","archived":false,"fork":false,"pushed_at":"2025-02-06T13:43:12.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-06T14:35:53.958Z","etag":null,"topics":["awscli","ec2-instance","explore","localstack","terraform"],"latest_commit_sha":null,"homepage":"","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/p4irin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-06T13:06:57.000Z","updated_at":"2025-02-06T13:43:16.000Z","dependencies_parsed_at":"2025-02-06T14:46:52.649Z","dependency_job_id":null,"html_url":"https://github.com/p4irin/explore_terraform_awscli_ec2_localstack","commit_stats":null,"previous_names":["p4irin/explore_terraform_awscli_ec2_localstack"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p4irin%2Fexplore_terraform_awscli_ec2_localstack","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p4irin%2Fexplore_terraform_awscli_ec2_localstack/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p4irin%2Fexplore_terraform_awscli_ec2_localstack/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p4irin%2Fexplore_terraform_awscli_ec2_localstack/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/p4irin","download_url":"https://codeload.github.com/p4irin/explore_terraform_awscli_ec2_localstack/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246871198,"owners_count":20847438,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["awscli","ec2-instance","explore","localstack","terraform"],"created_at":"2025-02-08T09:15:30.076Z","updated_at":"2025-04-02T18:42:07.575Z","avatar_url":"https://github.com/p4irin.png","language":"HCL","readme":"# Explore Terraform, awscli, EC2 instance on LocalStack\n\nCreate an AWS EC2 instance using the AWS CLI and Terraform on LocalStack\n\n## Pre-requisites\n\n* [LocalStack installed with a LOCALSTACK_AUTH_TOKEN](https://docs.localstack.cloud/getting-started/)\n* [AWS CLI installed](https://docs.localstack.cloud/user-guide/integrations/aws-cli/)\n* [An AMI (Amazone Machine Image)](https://docs.localstack.cloud/user-guide/aws/ec2/#:~:text=not%20be%20persisted.-,AMIs,-LocalStack%20utilizes%20a)\n* [Terraform installed](https://developer.hashicorp.com/terraform/install)\n\n### AMI\n\nLocalStack emulates EC2 instances using Docker containers. The images used for these containers are downloaded when installing LocalStack. The images are tagged with an AMI. Use the Ubuntu 22.04 image. Use following command to get the AMI of the Ubuntu image\n\n```bash\n$ docker images|grep localstack-ec2\nlocalstack-ec2/ubuntu-22.04-jammy-jellyfish  ami-df5de72bdb3b ...\nlocalstack-ec2/amazonlinux-2023              ami-024f768332f0 ...\n$\n```\nThe AMI to use in this case is `ami-df5de72bdb3b`.\n\n### AWS CLI configuration\n\nTouch these files\n* `~/.aws/config`\n* `~/.aws/credentials`\n\nAdd the following to `~/.aws/config`\n\n```bash\n[profile localstack]\nregion=us-east-1\noutput=json\nendpoint_url=http://localhost:4566\n```\n\nAdd the following to `~/.aws/credentials`\n\n```bash\n[localstack]\naws_access_key_id=test\naws_secret_access_key=test\n```\n\nWith above configuration you can use the `--profile` option with each `aws` command to run against LocalStack.\n\nE.g.:\n\n```bash\n$ aws --profile localstack ec2 run-instances ...\n```\n\n## General steps\n\n1. Run `$ localstack start`\n1. Create a key pair or import your existing public key\n1. Run the EC2 instance on LocalStack\n1. Run `$ localstack stop`\n\n_Note: By default access to port 22 is allowed so it's not necessary to add inbound rules_\n\n## AWS CLI\n\nStart LocalStack\n\n```bash\n$ localstack start\n```\n\n### Create a key pair or import your existing public key\n\nCreate a key pair\n\n```bash\naws --profile localstack ec2 create-key-pair \\\n    --key-name my-key \\\n    --query 'KeyMaterial' \\\n    --output text | tee key.pem\n```\n\nEC2 will output the private key to `key.pem`. The public key it will use for SSH public key authentication.\n\nIf you already have a key pair in `~/.ssh` you can just import the public key. Usually this is in the file `~/.ssh/id_rsa.pub`\n\n```bash\n$ aws --profile localstack ec2 import-key-pair \\\n    --key-name my-key\\\n    --public-key-material file://~/.ssh/id_rsa.pub\n\n```\n\n### Run the EC2 instance\n\n```bash\n$ aws --profile localstack ec2 run-instances \\\n  --image-id ami-df5de72bdb3b \\\n  --count 1 \\\n  --instance-type t3.nano\\\n  --key-name my-key\n```\n\n### Verify: connect to the instance using SSH\n\nIf you created a key pair as above connect with\n\n```bash\n$ ssh -i key.pem root@localhost\n```\n\nIf you imported your existing public key\n\n```bash\n$ ssh root@localhost\n```\n\n##\n\nStop LocalStack\n\n```bash\n$ localstack stop\n```\n\n## Terraform\n\nRefer to the `*.tf` files\n\nThe key pair resource is referenced in the instance resource. So, the dependency of the instance on the key pair is implied. The key pair is created before the instance.\n\nStart LocalStack\n\n```bash\n$ localstack start\n```\n\nInitialize the configuration\n\n```bash\n$ terraform init\n```\n\nThis takes a while. The provider plugins are downloaded and installed. When done initializing, you'll find a `.terraform` directory and a `.terraform.lock.hcl` lock file.\n\nSee what Terraform will do\n\n```bash\n$ terraform plan\n```\n\nDeploy to LocalStack\n\n```bash\n$ terraform apply\n```\nThis is when the state file is created.\n\nVerify deployment by connecting to the EC2 instance using SSH. The configuration uses an existing public key. So, you can connect with\n\n```bash\n$ ssh root@localhost\n```\n\nDiscard the resources managed by your configuration\n\n```bash\n$ terraform destroy\n```\n\nStop LocalStack\n\n```bash\n$ localstack stop\n```\n\n## References\n\n* [EC2](https://docs.localstack.cloud/user-guide/aws/ec2/)\n* [Terraform LocalStack integration](https://docs.localstack.cloud/user-guide/integrations/terraform/)\n* [Terraform resource: aws_instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance)","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fp4irin%2Fexplore_terraform_awscli_ec2_localstack","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fp4irin%2Fexplore_terraform_awscli_ec2_localstack","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fp4irin%2Fexplore_terraform_awscli_ec2_localstack/lists"}