Creating EC2 instance using terraform - amazon-ec2

I'm creating an ec2 instance using terraform and i have installed all prerequiste on my system, but unfourtunality when i run "terraform init" shown this message "
Initializing provider plugins...
Terraform has been successfully initialized!
but in directory there is no modules initilized and after run terraform plan there is no paln shows and also terraform apply command i receive this one "No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
"
I'm new in this field kindly i need some guide to lunche the ec2 instance

Related

How to build AWS image then use built image to run tests

I'm trying to use GitLab Pipelines to auto-build an AWS image with Packer, then auto-run tests with the given AMI string from the console output.
The Gitlab process looks like this:
Gitlab pipeline build stage: Buil an AWS image with Packer, which also installs an Apache service from provisioners.
Gitlab pipeline Test stage: Use the given AMI string from the console to launch a new AWS instance, then run some commands to verify if the Apache service up and run.
Gitlab pipeline Deployment stage
So far, from the build stage, everything works as expected, I can see the AMI string from the console, but I have no clue about the Test stage.
Console:
==> Wait completed after 4 minutes 33 seconds
==> Builds finished. The artifacts of successful builds are:
--> amazon-ebs: AMIs were created:
us-east-1: ami-0ea59f553d87cff53
--> amazon-ebs: AMIs were created:
us-east-1: ami-0ea59f553d87cff53
Here's my question:
How to get the AMI string and use it in the next stage(Test stage)?
Can I still use Packer for testing purposes in the Test stage, by starting up an instance with the given AMI, and running commands from provisioners? Or is there any other better way to do the test?
Note, I only want to test the built image from a new instance.
Any feedback is appreciated.

terraform wont download plugin without sudo

I am trying to run the terraform code and running the terraform init but I am running into issues.
As you can see, when I run with sudo, it has no issues, but without it, it has. I am using mac os Mojave terraform 0.12. I checked the folder permissions and it is just fine.
once I run sudo terraform init, the other commands don't need the sudo command.
Initializing the backend...
Initializing provider plugins...
- Checking for available provider plugins...
Registry service unreachable.
This may indicate a network issue, or an issue with the requested Terraform Registry.
Registry service unreachable.
This may indicate a network issue, or an issue with the requested Terraform Registry.
Error: registry service is unreachable, check https://status.hashicorp.com/ for status updates
Error: registry service is unreachable, check https://status.hashicorp.com/ for status updates
C02Z1BCSLVCG:blue-deployment shakyas$ sudo terraform init
Password:
Initializing the backend...
Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "aws" (hashicorp/aws) 2.42.0...
- Downloading plugin for provider "template" (hashicorp/template) 2.1.2...
The following providers do not have any version constraints in configuration,
so the latest version was installed.
To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.
* provider.aws: version = "~> 2.42"
* provider.template: version = "~> 2.1"
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
I had the same issue, and I resolved it with removing a lot of certificates in my macOS Keychain.
It sound weird and I still don't understand why it works, but it works for others people too : https://discuss.hashicorp.com/t/error-when-running-terraform-init/3135/6

Terraform behind the password protected proxy command

I have set the proxy in command as following
set HTTP_PROXY=http://user:passowrd#host.com:8080
set HTTPS_PROXY=https://user:passowrd#host.com:8080
set HTTP_USER=myuser
set HTTP_PASSWORD=mypwd
and future more I have set environment variable as HTTP_PROXY, HTTPS_PROXY, HTTP_USER, HTTP_PASSWORD
Somehow still getting following error
>terraform init
Initializing the backend...
Initializing provider plugins...
- Checking for available provider plugins...
Registry service unreachable.
This may indicate a network issue, or an issue with the requested Terraform Registry.
Error: registry service is unreachable, check https://status.hashicorp.com/ for status updates
please note that https://status.hashicorp.com/ having access behind the proxy.
but I am not sure terraform init actually which URL/service API is getting access
Working for me with proxy:
C:\Users\xxxx\Desktop\VMWare_Scripts\Terraform>set HTTP_PROXY=http://xxxx:8080
C:\Users\xxxx\Desktop\VMWare_Scripts\Terraform>terraform init
Initializing the backend...
Initializing provider plugins...
Finding latest version of hashicorp/vsphere...
Installing hashicorp/vsphere v1.24.1...
Installed hashicorp/vsphere v1.24.1 (signed by HashiCorp)
The following providers do not have any version constraints in configuration,
so the latest version was installed.
To prevent automatic upgrades to new major versions that may contain breaking
changes, we recommend adding version constraints in a required_providers block
in your configuration, with the constraint strings suggested below.
hashicorp/vsphere: version = "~> 1.24.1"
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Modify user_data after stopping aws EC2 with ansible playbook(ec2 or ec2_instance module)

I have a EC2 instance that's already launched using ansible ec2 module having user_data(say data1). Stopped the EC2 instance, now I want to modify the user_data(say data2) and start the instance. Giving modified user_data but its not getting reflected on aws.
To summarize, How to modify user_data of stopped aws EC2 using ansible script(with ec2 or ec2_instance) script.
By default, user data scripts and cloud-init directives run only during the boot cycle when you first launch an instance. You can update your configuration to ensure that your user data scripts and cloud-init directives run every time you restart your instance.
User Data for every restart

InvalidIamUserArnException when registering on prem instance

No matter what instance name I choose, whenever I perform the following on an on prem instance:
aws deploy register --instance-name test --tags "Key=Name,Value=test" --region us-west-2 --debug
The following exception is thrown (always):
2016-04-12 11:02:52,625 - MainThread - awscli.errorhandler - DEBUG - HTTP Response Code: 400
ERROR
A client error (InvalidIamUserArnException) occurred when calling the RegisterOnPremisesInstance operation: Iam User ARN
arn:aws:iam::xxx:user/AWS/CodeDeploy/test is not in a valid format
Register the on-premises instance by following the instructions in "Configure Existing On-Premises Instances by Using AWS CodeDeploy" in the AWS CodeDeploy User Guide.
Despite this error, the user gets created on amazon, and I can continue to register the on prem instance with the following:
aws deploy register-on-premises-instance --instance-name test --iam-user-arn arn:aws:iam::xxx:user/test
aws deploy install --override-config --config-file codedeploy.onpremises.yml --region us-west-2 --agent-installer s3://aws-codedeploy-us-west-2/latest/codedeploy-agent.msi
The instance is registered and the user is created, but when deploying to it, I always get "No hosts succeeded". The logs for the codedeploy agent show no errors.
I am not sure whats happening here either since no logs on either end, in codedeploy console or on the on prem machine codedeploy agent. Any ideas?
Please note I am using Windows Embedded Standard 2010 (which is not in the supported list) with the latest version of aws cli but I have successfully deployed to it in the past (with previous version of aws cli).
Figured it out, seems to be broken* if you try and let 'aws deploy register' create IAM user for you. However, if you create the user first (via console or aws cli), then it will work.
You can pass in the option '--iam-user-arn arn:aws:iam::xxx:user/OnPremCodeDeploy' with the 'aws deploy register' command afterwards.
I created the on prem yml manually with the correct access keys from manually creating user and then finally ran:
aws deploy install --overide-config --config-file conf.onpremises.yml --region us-west-2 --agent-installer s3://aws-codedeploy-us-west-2/latest/codedeploy-agent.msi
* at least w/ codedeployagent OFFICIAL_1.0.1.950_msi and windows embedded
Could you check if the IAM user you registered the on-premises instance with CodeDeploy has proper permissions? Including the following.
"iam:CreateAccessKey",
"iam:CreateUser",
"iam:DeleteAccessKey",
"iam:DeleteUser",
"iam:DeleteUserPolicy",
"iam:ListAccessKeys",
"iam:ListUserPolicies",
"iam:PutUserPolicy",
"iam:GetUser"
This can also be referred here: http://docs.aws.amazon.com/codedeploy/latest/userguide/how-to-configure-on-premises-host.html#how-to-configure-on-premises-host-prerequisites

Resources