EC2 Instance with an Admin Role
Published: 2023-07-18 13:08:22

Howdy!!! 
Some time back we learnt how to create and EC2 Instance and we even connected to it, don't remember? Check it out [here](https://dev.to/jeptoo/how-to-create-ec2-instance-ubuntu-2204-on-aws-and-connect-via-ssh-using-pem-492o).

- Today we are going to create an Instance based on Amazon Linux AMI that will be connected via SSH. Using Security Groups, you will ensure that access to the instance is limited to your IP address only.
- The CLI will be pre-installed on the instance by default. This instance only needs permissions assigned. Once the instance is up and running, create an IAM role with admin access for your account. Add the role to your EC2 after that.

## Table of Content
- [Objectives](#objectives)
- [Create a default Virtual Private Cloud](#create-a-default-virtual-private-cloud)
- [Launch an EC2 instance](#launch-an-ec2-instance)
- [Create an IAM Role](#create-an-iam-role)
- [Attach the Role to the EC2 Instance](#attach-the-role-to-the-ec2-instance)
- [Connect to your EC2 instance](#connect-to-your-ec2-instance)
- [Conclusion](#conclusion)

##Objectives
By the end of this article, you'll be able to:

- Launch a secure EC2 instance.
- Create IAM role with admin previleges
- Attach the IAM role to the your Instance.
- Connect to your EC2 instance via SSH
- Use CLI tool in the Instance.


##Create a default Virtual Private Cloud
- [VPC](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html) is a private cloud computing environment contained  within a public cloud and once can launch AWS resources in.

- Check in your account if you already have a [default VPC](https://docs.aws.amazon.com/vpc/latest/userguide/default-vpc.html) and if not, go to the VPC dashboard and create a default VPC.

![VPC](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j15pkcj57gk5u0rky0ge.png)

##Launch an EC2 instance
- I already have an article that covers [launching EC2](https://dev.to/jeptoo/how-to-create-ec2-instance-ubuntu-2204-on-aws-and-connect-via-ssh-using-pem-492o) but just to touch briefly on the steps:

![configurations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/90zm7pzr0kpwsfjmqg5d.png)

- Under security limit access to your IP address only.

![access](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r135izcxikmun108h37e.png)

- If you do not have a SSH key be sure to download a new one. 

<u>**N/B**</u>
**This key-pair will allow you to log into your instance, using SSH, from your local machine. Save the key-pair carefully, because the same private key cannot be re-generated.**


- Once you have launched your Instance, verify that it is running successfully.

##Create an IAM Role
[Identity and Access Management](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) is used to specify who and what can access services and resources in AWS.

- On the IAM dashboard, select **Roles**
- Click on **Create role** button

![create role](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0m6de35tvg1s08bnas45.png)

- Select **AWS service** as the trusted entity and **EC2** as the use case. This will allow the instance to whom the role will be attached to to be able to call any AWS service on your behalf.

![role1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lu55zvm2cezy0ix9rsgo.png)

- Under the permissions, search for **AdministratorAccess** in **Filter policies** textbox to apply to the role.

![permissions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/et8vlxt6ou0qf7vlszn7.png)

- Under the review section, provide a name to the new role.

![role name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/12uhvquxf4giab3r4c6u.png)


##Attach the Role to the EC2 Instance
- On the EC2 dashboard, check on the running instances and select the checkbox on the Instance we created earlier.
- Click the **Actions** button which will open a drop-down options, select the **Security** → **Modify IAM role**

![attach](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/79ng429c6qmuhpeciaq0.png)

- Select and apply the newly created role to your Instance

![select role](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rpgkvj5uv21hu89ehhk6.png)

##Connect to your EC2 instance
- We are going to connect to the EC2 instance using SSH, under **Actions**, click on **Connect**

![connect](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mh4akhvhd65azu9ay86s.png)

- Follow the SSH steps to connect to the Instance.

![ssh](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4ky87sq0t0fhf7n6kl3.png)

- After connecting to the instance, you need to verify installation of AWS CLI


![terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7f1ijg0ndebedxgeyobx.png)



##Conclusion
- This is a practical method for having a well configured, secure server that you can use for testing without worrying about credentials.

_Happy cloud  adventures!_