-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathaws_cli.sh
More file actions
202 lines (176 loc) · 9.57 KB
/
aws_cli.sh
File metadata and controls
202 lines (176 loc) · 9.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
##CLI Doc: https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html
#01. Configuration
##00.01: Linux Installation
sudo yum install epel-release
sudo yum install python-pip
sudo pip install awscli
### Check if aws installation succeeds: #
#02. IAM/STS
#02.01. Determine the identity currently used:
aws sts get-caller-identity
##02.02. Authentication by secret keys: Not recommended (see reasons, below)!
aws configure
#... AWS Secret Access Key [None]: enter Key
#... AWS Secret Access Key [None]: enter Key
#... Default region name [Access Key]: us-east-1
#... Default output format [exist]: json
#... If aws is hacked, secret keys will be found in ~/.aws folder. #
cd ~/.aws
ls
#... config credentials
nano credentials
#... Remove the ~/.aws/credentials File
rm ~/.aws/credentials
##02.03. Authentication by Creating a profile with AWS Access Key ID and Secret Access Key:
aws configure --profile myProfileNonDefault
#... AWS Access Key ID [None]: AKIAR5TTEBC52524V3ZV
#... AWS Secret Access Key [None]: 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
#... Default region name [None]: us-east-1
#... Default output format [None]: json
#... Test the Configuration: Determine the identity currently used in the order: Environment variables should be returned.
aws sts get-caller-identity
#... Test the profile: run aws sts commands with the profile create above
aws sts get-caller-identity --profile myProfileNonDefault
#... Run a command as Profile
aws s3 ls --profile myProfileNonDefault
##02.04. Authentication by login and password to an EC2 instance:
ssh cloud_user@107.21.4.52
#... Password:
##02.05. Authentication by Environment Variable Configuration:
export AWS_ACCESS_KEY_ID=AKIAR5TTEBC523CIE3W6
export AWS_SECRET_ACCESS_KEY=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
aws sts get-caller-identity #Test the Configuration: Determine the identity currently used in the order
#... Clear the Environment Variables
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
# 02.06. Authentification by using ssh agent forwarding
#... Adding the private key (PEM file) to the key chain
ssh-add -k MyKeyPair.pem
#... Check whether the private key is properly added to the key chain
ssh-add -L
#... Access the Bastion Host (Public instance)
ssh -A ec2-user@ec2-3-90-235-45.compute-1.amazonaws.com
#... Access the private instance
ssh ec2-user@10.0.0.25
#... Get the policy (role) details, including the current version
aws iam get-policy --policy-arn "arn:aws:iam::241021092242:policy/DevS3ReadAccess"
aws iam get-policy --policy-arn "arn:aws:iam::241021092242:role/DEV_ROLE"
#... Get the policy details of a specific version
aws iam get-policy-version --policy-arn "arn:aws:iam::241021092242:policy/DevS3ReadAccess" --version-id "v1"
#02.02. IAM Users
#02.03. IAM Groups
#02.04. IAM Groups
#... List all attached permission policies attached to a role
aws iam list-attached-role-policies --role-name DEV_ROLE
#... 02.04.01 Create IAM EC2 Role:
#... Create a Json Trust Policy for an EC2 role
cat ./service_compute_ec2/roles/trust_policy_ec2.json
#... Create the IAM role with the Trust Policy only
aws iam create-role --role-name DEV_ROLE --assume-role-policy-document file://service_compute_ec2/roles/trust_policy_ec2.json
#... Create a Json role permission policy
cat ./service_compute_ec2/roles/dev_s3_read_access.json
#... Create a Managed Policy with the json file
aws iam create-policy --policy-name DevS3ReadAccess --policy-document file://service_compute_ec2/roles/dev_s3_read_access.json
#... Attach Managed Policy to the Role created above:
aws iam attach-role-policy --role-name DEV_ROLE --policy-arn "arn:aws:iam::241021092242:policy/DevS3ReadAccess"
#03. EC2:
#03.1 Stress test an EC2 instance:
sudo amazon-linux-extras install epel -y
sudo yum install stress -y
stress --cpu 2 --timeout 30
#... Get user data from an EC2 instance:
curl http://169.254.169.254/latest/user-data
#... Get EC2 meta-data from an EC2 instance: get available options
curl http://169.254.169.254/latest/meta-data/
#... Get a specific EC2 meta-data from an EC2 instance (E.g., local-ipv4):
curl http://169.254.169.254/latest/meta-data/local-ipv4/
#06. VPC:
#... 06.01. Internet Gateway
#....... Attach a detached an IGW to a VPC
aws ec2 attach-internet-gateway --vpc-id "vpc-092020d901c843073" --internet-gateway-id "igw-0bc2368fff8eeddbb" --region us-east-1
#08. S3:
#08.1 Manage s3 buckets:
#..... Select all S3 buckets
aws s3 ls
#..... Create an s3 bucket:
aws s3
#..... Delete an empty bucket
aws s3 rb s3://my-bucket
#..... Delete a non empty bucket (non versioned objects)
aws s3 rb s3://my-bucket --force
#08.3 List object of a bucket
aws s3 ls --recursive s3://my-bucket
#08.2 Manage objects in a bucket:
#..... Copy a file from EC2 to S3 bucket
aws s3 cp index.html s3://my-bucket
#..... Multipart uploading (Create a 10GB file + Multipart uploading)
dd if=/dev/zero of=10GBfile.data bs=1M count=10240
aws s3 cp ./10GBfile.data s3://my-bucket/
#..... Delete an object in S3
aws s3 rm s3://my-bucket/myfile
#..... Presigned URLs: Create a presigned URL that expires in 1 hour:
aws S3 presign s3://my-bucket/myfile --expires-in 3600
#...... Invalidate a presigned URL
#...... It's not possible!
#...... Copy a folder To an S3 bucket:
aws s3 cp --recursive ./my-folder s3://my-bucket
#...... Synchronize a folder that is previously copied to S3
aws s3 sync ./my-folder s3://my-bucket
#10. EFS:
#10.1 Install Amazon EFS utilities
sudo yum install -y amzon-efs-utils
#10.2 Create a file system which type is EFS:
sudo mkdir /mnt/myefs
sudo mount -t efs [fs-ID]:/ /mnt/myefs
cd /mnt/myefs
#16. DynamoDB:
aws dynamodb scan --table-name my-Table
#24. SNS:
#25. SQS:
aws sqs get-queue-attributes --queue-url https://URL --attribute-names All
aws sqs send-message --queue-url https://URL --message-body "INSERTMESSAGE"
aws sqs receive-message --queue-url https://URL
aws sqs delete-message --queue-url https://URL --receipt-handle "INSERTHANDLE"
aws sqs receive-message --wait-time-seconds 10 --max-number-of-messages 10 --queue-url https://URL
aws sqs --region us-east-1 receive-message --wait-time-seconds 10 --max-number-of-messages 10 --queue-url https://URL
aws sqs delete-message --queue-url https://URL --receipt-handle "INSERTHANDLE"
#25. 2: SQS with VPC interface endrpoint (from a prive instance)
#...... VPC interface endpoint is on a public subnet
#...... The private instance is a private subnet within the same AZ as the public subnet above
#...... VPC interface endpoint has a SG with All traffic allowed from the SG of the private instance
#...... Endpont enbaled????
#...... I wasn't able to connect to the SQS queue by using the commands above
#...... I was able to receive/send messages with the commands below
aws sqs send-message --region us-east-1 --endpoint-url https://sqs.us-east-1.amazonaws.com/ --queue-url https://sqs.us-east-1.amazonaws.com/944550713856/SQSEndpointInterfaceTest --message-body "Hello from Amazon SQS."
aws sqs receive-message --region us-east-1 --endpoint-url https://sqs.us-east-1.amazonaws.com/ --queue-url https://sqs.us-east-1.amazonaws.com/944550713856/SQSEndpointInterfaceTest
#...... aws configure region is set with us-east-1
aws sqs send-message --endpoint-url https://sqs.us-east-1.amazonaws.com/ --queue-url https://sqs.us-east-1.amazonaws.com/944550713856/SQSEndpointInterfaceTest --message-body "Hello from Amazon SQS."
aws sqs receive-message --endpoint-url https://sqs.us-east-1.amazonaws.com/ --queue-url https://sqs.us-east-1.amazonaws.com/944550713856/SQSEndpointInterfaceTest
#...... using the non AZ-related interface endpoint DNS:
aws sqs send-message --endpoint-url https://vpce-02346a53c7b736f32-coolless.sqs.us-east-1.vpce.amazonaws.com --queue-url https://vpce-02346a53c7b736f32-coolless.sqs.us-east-1.vpce.amazonaws.com/SQSEndpointInterfaceTest --message-body "Hello from Amazon SQS."
#...... using the AZ-a interface endpoint DNS:
aws sqs send-message --endpoint-url https://vpce-02346a53c7b736f32-coolless-us-east-1a.sqs.us-east-1.vpce.amazonaws.com --queue-url https://vpce-02346a53c7b736f32-coolless-us-east-1a.sqs.us-east-1.vpce.amazonaws.com/SQSEndpointInterfaceTest --message-body "Hello from Amazon SQS."
#31. KMS:
#31.1. Create a Customer Managed CMK
#31.1.1 Linux/Mac OS
aws kms create-key --description "LA KMS DEMO CMK"
aws kms create-alias --target-key-id XXX --alias-name "alias/lakmsdemo" --region us-east-1
echo "this is a secret message" > topsecret.txt
aws kms encrypt --key-id KEYID --plaintext file://topsecret.txt --output text --query CiphertextBlob
aws kms encrypt --key-id KEYID --plaintext file://topsecret.txt --output text --query CiphertextBlob | base64 --decode > topsecret.encrypted
aws kms decrypt --ciphertext-blob fileb://topsecret.encrypted --output text --query Plaintext | base64 --decode
aws kms generate-data-key --key-id KEYID --key-spec AES_256 --region us-east-1
#31.1.2. Windows:
aws kms create-key --description "LA KMS DEMO CMK"
aws kms create-alias --target-key-id XXX --alias-name "alias/lakmsdemo" --region us-east-1
echo "this is a secret message" topsecret.txt
aws kms encrypt --key-id KEYID --plaintext file://topsecret.txt --output text --query CiphertextBlob
aws kms encrypt --key-id KEYID --plaintext file://topsecret.txt --output text --query CiphertextBlob > topsecret.base64.encrypted
certutil -decode topsecret.base64.encrypted topsecret.encrypted
aws kms decrypt --ciphertext-blob fileb://topsecret.encrypted --output text --query Plaintext > topsecret.decrypted.base64
certutil topsecret.decrypted.base64 topsecret.decrypted
#31.2. Create a DEK
#31.2.1 Linux/Mac OS:
aws kms generate-data-key --key-id KEYID --key-spec AES_256
#31.2.2. Windows:
aws kms generate-data-key --key-id KEYID --key-spec AES_256