Launch EC2 instance 1 (App server with App and Syslogs, and log delivery agents)

"Instance 1" will be the server with the application logs and syslogs. We will install the CloudWatch Logs agent, and filebeat agent on this EC2 instance.

Environment variables

ESTEST_INSTANCE_1_KEYPAIR=/Users/tranjim/Desktop/data/tools/__credentials/jtkeypair_pdx.pem
ESTEST_INSTANCE_1_NAME=ESTEST_Instance_1-$(date "+%M%S")
ESTEST_INSTANCE_1_AMI=ami-5189a661 # Ubuntu server

ESTEST_INSTANCE_1_IAM_ROLE_NAME=ESTEST_Instance1-IAM_Role
ESTEST_INSTANCE_1_IAM_POLICY_NAME=ESTEST_Instance1-IAM_Policy
ESTEST_INSTANCE_1_PROFILE_NAME=ESTEST_Instance1-Instance_Profile

Create an IAM Role for EC2

aws iam create-role \
        --role-name "$ESTEST_INSTANCE_1_IAM_ROLE_NAME" \
        --output text \
        --query 'Role.Arn' \
        --assume-role-policy-document '{
              "Version": "2012-10-17",
              "Statement": [{
                  "Effect": "Allow",
                  "Principal": { "Service": "ec2.amazonaws.com"},
                  "Action": "sts:AssumeRole"
              }]
            }'

ESTEST_INSTANCE_1_IAM_ROLE_ARN=arn:aws:iam::$ESTEST_ACCOUNT_ID:role/$ESTEST_INSTANCE_1_IAM_ROLE_NAME    

aws iam put-role-policy \
        --role-name   "$ESTEST_INSTANCE_1_IAM_ROLE_NAME"   \
        --policy-name "$ESTEST_INSTANCE_1_IAM_POLICY_NAME" \
        --policy-document '{
                  "Version": "2012-10-17",
                  "Statement": [{
                      "Effect": "Allow",
                      "Resource": [ "arn:aws:logs:*:*:*" ],
                      "Action": [
                        "logs:CreateLogGroup",
                        "logs:CreateLogStream",
                        "logs:PutLogEvents",
                        "logs:DescribeLogStreams"
                      ]
                  }]
               }'

aws iam create-instance-profile --instance-profile-name $ESTEST_INSTANCE_1_PROFILE_NAME

aws iam add-role-to-instance-profile \
    --instance-profile-name $ESTEST_INSTANCE_1_PROFILE_NAME \
    --role-name $ESTEST_INSTANCE_1_IAM_ROLE_NAME

Create a security group

aws ec2 create-security-group --group-name $ESTEST_INSTANCE_1_NAME --description "$ESTEST_INSTANCE_1_NAME"
aws ec2 authorize-security-group-ingress --group-name $ESTEST_INSTANCE_1_NAME --protocol tcp --port 22 --cidr 0.0.0.0/0

Launch the instance, and tag the instance

ESTEST_INSTANCE_1_ID=$(aws ec2 run-instances \
         --image-id $ESTEST_INSTANCE_1_AMI \
         --count 1 \
         --instance-type m3.medium \
         --key-name jtkeypair_pdx \
         --security-groups $ESTEST_INSTANCE_1_NAME \
         --iam-instance-profile Name=$ESTEST_INSTANCE_1_IAM_INSTANCE_PROFILE_NAME \
         --region us-west-2 \
         | jq --raw-output .Instances[0].InstanceId) && echo $ESTEST_INSTANCE_1_ID

aws ec2 create-tags --resources $ESTEST_INSTANCE_1_ID --tags Key=Name,Value=$ESTEST_INSTANCE_1_NAME

Login to the Ubuntu instance

ESTEST_INSTANCE_1_DNS=$(aws ec2 describe-instances --instance-ids $ESTEST_INSTANCE_1_ID | jq --raw-output .Reservations[0].Instances[0].PublicDnsName) && echo $ESTEST_INSTANCE_1_DNS

ssh -i $ESTEST_INSTANCE_1_KEYPAIR ubuntu@$ESTEST_INSTANCE_1_DNS

### (One time setup)
# change prompt color to green
echo 'export PS1="\[\033[0;32m\] INSTANCE 1 (Server sending syslogs and applogs) :[\w]  \[\033[0m\]"' \
         >> ~/.bash_profile &&  source ~/.bash_profile