airflow์ ์ด์ํ๋ ec2 instance๊ฐ ์์ฃผ ๋ฌด๊ฒ๊ฒ ๋์๊ฐ๊ณ ์๋ ์ํฉ์ด๋ค..
๊ทธ๋์ ๊ทธ๋ฐ์ง ์ผ์ฃผ์ผ์ ํ๋ฒ์ฉ airflow๊ฐ ์คํ๋๊ณ ์์ง ์๋ ๊ฒฝ์ฐ๋ค์ด ์๋ค.
์์ธ ํ์ ์ ์ ๋๋ก ํ๊ณ ๊ฐ์ฑํด์ผํ์ง๋ง ์ฐ์ ์ง๊ธ ๋น์ฅ ๋์จ ์กฐ์น๋ก๋ ์ฃผ๊ธฐ์ ์ผ๋ก ec2 instance๋ฅผ reboot ํ์๋ ์๊ฒฌ!
๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ผ๋ก๋ rebootํ๋ lambda function์ ์์ฑํ ํ
CloudWatch Events (EventBridge) ๋ก ๊ท์น์ ์์ฑํด์, ๊ณ ์ ๊ฐ๊ฒฉ์ผ๋ก ํธ๋ฆฌ๊ฑฐ ์คํํ๋ ๋ฐฉ๋ฒ!
๊ทผ๋ฐ CloudWatch Events (EventBridge) ์ฌ์ฉํ๋ฉด ์ถ๊ฐ ์๊ธ ๋ฐ์ํ๊ธฐ์ .. crontab ์ผ๋ก ๊ด๋ฆฌํ๋ ๊ฑธ๋ก ๊ฒฐ์ ํ๋ค.
โ ์งํ
1. AWS lambda function ์์ฑ
2. ์์ฑํ lambda function์ ec2 instance reboot ๊ถํ ๋ถ์ฌ
2-1. ec2 instance์ ์ ๊ทผํ๋ Full Access ๊ถํ ๋ถ์ฌ
2-2. ํด๋นํ๋ instance๋ง rebootํ ์ ์๋ ์ต์ ๊ถํ ๋ถ์ฌ
3. lambda function ์คํ ํ์ธ
4. crontab ์ถ๊ฐํ๊ธฐ
1๏ธโฃ AWS lambda function ์์ฑํ๊ธฐ
reboot-airflow ๋ผ๋ ์ด๋ฆ์ lambda function์ ์์ฑํ๋ค.
ํด๋นํ๋ region ๊ณผ instance id๋ฅผ ๋ฃ์ด์ค๋ค.
import boto3
def lambda_handler(event, context):
ec2 = boto3.client('ec2', region_name='ap-northeast-2') # region
instance_ids = ['i-000000000aaaaaaaa'] # instance id
response = ec2.reboot_instances(InstanceIds=instance_ids)
return response
์ด lambda function์ ์คํํ๋ฉด ์๋์ ๊ฐ์ message๋ฅผ ๋ฐ์ ์ ์๋ค.
"errorMessage": "An error occurred (UnauthorizedOperation) when calling the RebootInstances operation: You are not authorized to perform this operation. User: arn:aws:sts::425405151003:assumed-role/reboot-airflow-role-4ofplg3v/reboot-airflow is not authorized to perform: ec2:RebootInstances on resource: arn:aws:ec2:ap-northeast-2:425405151003:instance/i-00000000aaaaaaaa because no identity-based policy allows the ec2:RebootInstances action. Encoded authorization failure message: ...",
"errorType": "ClientError",
"requestId": "89210677-e54f-44a7-a472-d538ab1c4e22",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 8, in lambda_handler\n response = ec2.reboot_instances(InstanceIds=instance_ids)\n",
" File \"/var/lang/lib/python3.12/site-packages/botocore/client.py\", line 565, in _api_call\n return self._make_api_call(operation_name, kwargs)\n",
" File \"/var/lang/lib/python3.12/site-packages/botocore/client.py\", line 1021, in _make_api_call\n raise error_class(parsed_response, operation_name)\n"
]
> ํด์ํ๋ฉด ec2 instance๋ฅผ rebootํ ์ ์๋ ๊ถํ์ด ์๋ค๋ ์๋ฏธ์ด๋ค.
2๏ธโฃ lambda function ์ ๊ถํ ๋ถ์ฌ
2๏ธโฃ-1๏ธโฃ ec2 instance์ ์ ๊ทผํ๋ Full Access ๊ถํ ๋ถ์ฌ
IAM > Roles > reboot-airflow-role-4ofplg3v ์ ํ
Permissions > Add permissions > Attach policies ์ ํ
AmazonEC2FullAccess ์ถ๊ฐ
EC2์ Accessํ ์ ์๋ ๊ถํ ์ถ๊ฐ๋จ
๊ทผ๋ฐ ์ด ๊ถํ์ EC2์ ๊ด๋ จ๋ Full Access์ ๊ดํ ๊ถํ์ด๋ผ ์ํํ ์ ์๊ธฐ์
airflow ์๋ฒ๋ง rebootํ ์ ์๋ ์ต์ ๊ถํ๋ง ๋ถ์ฌํ๋ ค ํจ
2๏ธโฃ-2๏ธโฃ ํด๋นํ๋ instance๋ง rebootํ ์ ์๋ ์ต์ ๊ถํ ๋ถ์ฌ
IAM > Policies > Create policy : ๊ถํ ์ถ๊ฐํ๊ธฐ
JSON์ผ๋ก ์ ํํ์ฌ ์ ๋ ฅํ๋ค.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "ec2:RebootInstances",
"Resource": "arn:aws:ec2:ap-northeast-2:111111111111:instance/i-00000000aaaaaaaa" # ์์
}
]
}
์ถ๊ฐํ๋ policy์ ์ด๋ฆ๊ณผ ์ค๋ช ์ ์ ๋ ฅํ๋ค.
reboot-airflow-role-4ofplg3v ์ ์์ฑํ ๊ถํ์ ์ถ๊ฐํด์ค๋ค.
3๏ธโฃ lambda function ์คํ ํ ์คํธ
์ฑ๊ณตํ๋ค๋ response๋ฅผ ๋ฐ์ ์ ์๋ค. → ์ ์์ ์ผ๋ก reboot
{
"ResponseMetadata": {
"RequestId": "29c08d4e-9d77-4d77-a318-329efbaca204",
"HTTPStatusCode": 200,
"HTTPHeaders": {
"x-amzn-requestid": "29c08d4e-9d77-4d77-a318-329efbaca204",
"cache-control": "no-cache, no-store",
"strict-transport-security": "max-age=31536000; includeSubDomains",
"content-type": "text/xml;charset=UTF-8",
"content-length": "219",
"date": "Tue, 29 Oct 2024 00:35:42 GMT",
"server": "AmazonEC2"
},
"RetryAttempts": 0
}
}
4๏ธโฃ crontab ์ ์ถ๊ฐ
reboot.py
import boto3
def trigger_lambda(function_name):
lambda_client = boto3.client('lambda',
region_name='ap-northeast-2',
aws_access_key_id='aws_access_key_id',
aws_secret_access_key='aws_secret_access_key'
)
response = lambda_client.invoke(
FunctionName=function_name,
InvocationType='RequestResponse'
)
print(response['Payload'].read().decode('utf-8'))
function_name = 'reboot-airflow'
trigger_lambda(function_name)
reboot.sh
#!/bin/bash
cd /home/ubuntu
python reboot.py
crontab
# reboot server
35 1 * * 6 /home/ubuntu/reboot.sh
๋๋ ๋งค์ฃผ ํ ์์ผ 1์ 35๋ถ์ ์คํ์ํฌ ๊ณํ์ด๊ธฐ ๋๋ฌธ์ ์์ ๊ฐ์ด ์ค์ ํ๋ค
- crontab ์ค์ : ์์์ ๋ถํฐ ์์๋๋ก
- ๋ถ (0 - 59)
- ์ (0 - 23)
- ๋ ์ง (1 - 31)
- ์ (1 - 12)
- ์์ผ (0 - 7, 0๊ณผ 7์ ์ผ์์ผ)
'๐ฉ๐ปโ๐ป > aws' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[AWS] SNS Topics์ ๋ฑ๋กํ Pending confirmation ์ํ์ Subscription ์ญ์ (0) | 2024.08.07 |
---|---|
[AWS] EC2 Instance์ Alarms ์ถ๊ฐํ๊ธฐ (0) | 2024.08.06 |
[EC2 Instance] file descriptor leak issue (0) | 2024.08.06 |