목차
0. 개요
- IAM User 를 새로 생성해 IAM Group 에 소속시키고 이 그룹에 역할 및 인라인 정책을 부여하여 AWS Resource 에 접근 및 동작을 확인하는 테스트
- Private ECR, SSM, S3, DynamoDB 등의 스토리지 리소스에 접근해서 값을 읽거나 업로드 하는 작업이 가능하도록 권한 부여
1. 테스트 환경
- IAM User 및 IAM Group 생성 후 access key 생성
- Private ECR Image
- S3 bucket - default setting
- DynamoDB Table 생성 후 Item 생성
- SSM Parameter Store 에 standard parameter 생성
2. 테스트 진행
2.1 ECR 이미지 가져오기
- 미리 생성해놓은 Private ECR 에서 이미지를 가져오는 작업 테스트
- 로컬 우분투에 aws-cli 2 를 설치한 후 configure 를 통해 새로운 IAM 유저 등록
IAM-test$ aws configure
AWS Access Key ID [****************SK6Z]: my-access-key
AWS Secret Access Key [****************w3wC]: my-secret-key
Default region name [ap-northeast-2]:
Default output format [json]:
인라인 정책 생성
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"ecr:DescribeImages",
"ecr:DescribeRepositories",
"ecr:ListImages",
"ecr:BatchCheckLayerAvailability",
"ecr:GetRepositoryPolicy"
],
"Resource": "arn:aws:ecr:ap-northeast-2:553149402753:repository/ecr-k3spod-prod-ethauto"
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": [
"ecr:GetRegistryPolicy",
"ecr:DescribeRegistry",
"ecr:GetAuthorizationToken"
],
"Resource": "*"
}
]
}
확인
$ aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin 553149402753.dkr.ecr.ap-northeast-2.amazonaws.com
WARNING! Your password will be stored unencrypted in /home/ubuntu/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
$ docker pull 553149402753.dkr.ecr.ap-northeast-2.amazonaws.com/ecr-k3spod-prod-ethauto:v.1.1.5
v.1.1.5: Pulling from ecr-k3spod-prod-ethauto
...
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
553149402753.dkr.ecr.ap-northeast-2.amazonaws.com/ecr-k3spod-prod-ethauto v.1.1.5 4aa141398ba6 2 days ago 1.8GB
2.2 S3 bucket에 파일 업로드
기본값으로 bucket 을 하나 생성하고 이 bucket 에 dummy file 을 업로드 하는 작업 테스트
#테스트 코드
import boto3
def send_logs_to_s3(file_path):
client = boto3.client('s3')
response = client.put_object(
Bucket='test-bucket-cyaninn',
Key='dummy-file-from-local.txt',
Body=open(file_path, 'rb') # 파일 내용을 업로드
)
return response
print(send_logs_to_s3('/home/cyaninn/Cryptocurrency-autotrade-pyupbit/test/IAM-test/dummy-file.txt'))
인라인 정책
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::test-bucket-cyaninn/*"
}
]
}
확인
2.3 dynamoDB 테이블, SSM parameter 읽기
이 두 작업은 관리형 정책을 사용하기로 함
테스트 코드
import boto3
def get_parameter_fromSSM() :
ssm = boto3.client('ssm')
parameters=['test']
ssm_para=list()
for i in parameters:
response = ssm.get_parameter(
Name=i,
WithDecryption=True
)
ssm_para.append(response['Parameter']['Value'])
return ssm_para[0]
print(get_parameter_fromSSM())
def read_dynamoDB_table() :
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Table-ForEthauto-PROD-ethauto')
response = table.get_item(
Key={
'env': 'DEV'
}
)
item = response['Item']
test_item = item['mbti']
#predicted_end_price = item['endprice']
return test_item
print(read_dynamoDB_table())
결과 확인
$python3 iam-test.py
this is test parameter
entj
3. 참고
https://docs.aws.amazon.com/AmazonECR/latest/userguide/security_iam_id-based-policy-examples.html
Amazon Elastic Container Registry Identity-based policy examples - Amazon ECR
Amazon Elastic Container Registry Identity-based policy examples By default, users and roles don't have permission to create or modify Amazon ECR resources. They also can't perform tasks by using the AWS Management Console, AWS Command Line Interface (AWS
docs.aws.amazon.com
'AWS' 카테고리의 다른 글
ECS Anywhere ConnectTimeoutError (0) | 2024.07.21 |
---|---|
[Error] AWS Budget scope 에서 tag가 검색되지 않음 (0) | 2023.10.08 |
[error][cloudformation] Your requested instance type (t3a.small) is not supported (1) | 2023.09.04 |
lambda 함수에서 dynamoDB 테이블 수정 (0) | 2023.04.02 |
aws 교과서 수준 블로그 찾음 (0) | 2022.09.16 |