1. ๋ชฉ์ 

๐Ÿ’ก
์„œ๋ฒ„ ์šด์˜์ค‘ auto-scaling์œผ๋กœ scale-out์ด ๋ ์‹œ ๋ชจ๋“  ์„œ๋ฒ„๊ฐ€ ํŽ˜์ด์ง€ ์—…๋ฐ์ดํŠธ ๋˜๋Š”์ง€ ํ™•์ธ๊ณผ์ •์„ ๊ฑฐ์ณ ๋ณด์ž ์˜ˆ๋ฅผ ๋“ค์–ด) ํ™ˆํŽ˜์ด์ง€์˜ ์ด๋ฒคํŠธ ๊ธฐ๊ฐ„์— ์ฆ‰๊ฐ์ ์ธ ์—…๋ฐ์ดํŠธ๋ฅผ ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด sacle-out ๋ ๋–„๋งˆ๋‹ค EFS ๋ฅผ ํ†ตํ•ด ๋ชจ๋“  ์„œ๋ฒ„์˜ ํŽ˜์ด์ง€๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด๋ณด์ž

 

2.๊ตฌ์„ฑ
ec2 ์ƒ์„ฑ
  1. ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ํผ๋ธ”๋ฆญ์˜ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
    → ๊ธฐ๋ณธ์ ์ธ web๊ตฌ์„ฑ๊ณผ efs๋ฅผ mount ์‹œ์ผœ๋‘๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์˜ ํ…Œ์ŠคํŠธ ์ธ์Šคํ„ด์Šค
  1. ๊ฐ„๋‹จ indexํŽ˜์ด์ง€
    → ๊ธฐ๋ณธ์œผ๋กœ ๋“ค์–ด๊ฐ€ ์žˆ์„ ์›นํŽ˜์ด์ง€์ด๋‹ค.
EFS ์ƒ์„ฑ

๊ฐœ๋… ์ฐธ๊ณ  : https://practice.hooniworld.io/entry/AWS-EFS-๊ฐœ๋…-๋ฐ-๊ตฌ์„ฑ-๋ฐฉ๋ฒ•

EFS ์‚ฌ์šฉ ์˜ˆ์‹œ

  • EFS ์ƒ์„ฑ์‹œ VPC ์•ˆ์˜ AZ subnet๋‹น mount targer์ƒ์„ฑ
  • ๋‘๊ฐœ ์ด์ƒ์ผ ๊ฒฝ์šฐ ํ•œ๊ฐœ๋งŒ mount target ์ƒ์„ฑ ๊ฐ€๋Šฅ
  1. console ์„ ํ†ตํ•˜์—ฌ EFS ์ƒ์„ฑ
  1. ๋งˆ์šดํŠธ ํฌ์ธํŠธ
ec2-EFS ๋งˆ์šดํŠธ
  1. EFS์˜ ๊ฐ€์šฉ์˜์—ญ ํ™•์ธ
  1. ๋ณด์•ˆ๊ทธ๋ฃน ์„ค์ •
  1. nfs ํด๋ผ์ด์–ธํŠธ ์„ค์น˜
    sudo yum -y update  
    $  sudo reboot
    sudo yum -y install nfs-utils
  1. nfs ํด๋ผ์ด์–ธํŠธ๊ฐ€ ํƒ‘์žฌ๋˜์–ด์žˆ์œผ๋ฏ€๋กœ nfs ์ด์šฉ
  1. efs ํ˜ธ์ŠคํŠธ ๋“ฑ๋ก
    → auto-mount ๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•จ ์œ„์—์„œ ๋ณด์ด๋Š” mount ํฌ์ธํŠธ๋ช…์€ ๋„ˆ๋ฌด ๊ธธ๊ธฐ๋•Œ๋ฌธ ์‚ฌ์šฉ๋  ๊ฐ€์šฉ์˜์—ญ๋งŒ ์ง€์ •ํ•ด์ค€๋‹ค.
  1. auto mount๋ฅผ ์œ„ํ•œ ์„ค์ •
  1. ํ™•์ธ
efs ํŒŒ์ผ ์ƒ์„ฑํ›„ AMI ์ƒ์„ฑ
→ ํ•ด๋‹น ๋งˆ์šดํŠธ point๋กœ ์ด๋™ํ›„ ํŒŒ์ผ ์ƒ์„ฑ ๋’ค ํ•ด๋‹น ์ธ์Šคํ„ด์Šค ์ด๋ฏธ์ง€ ์ƒ์„ฑ
auto sacling ์ƒ์„ฑ
  1. ์œ„์—์„œ ์ƒ์„ฑ๋œ AMI๋ฅผ ์ด์šฉํ•˜์—ฌ ์‹œ์ž‘๊ตฌ์„ฑ ์ƒ์„ฑ
  1. SSM์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๊ธฐ๋•Œ๋ฌธ์— ํ•ด๋‹น role์„ ๋ถ€์—ฌ
    → ec2 ์ธ์Šคํ„ด์Šค๋“ค์ด ssm์„ ์‚ฌ์šฉํ•  ๊ถŒํ•œ์ด ์žˆ์–ด์•ผ ํ•˜๋ฏ€๋กœ iam์—์„œ ์—ญํ•  ์ƒ์„ฑ
  1. auto-scaling ์ƒ์„ฑ
    → efs ์‚ฌ์šฉ ๊ฐ€์šฉ์˜์—ญ์„ ๋งž์ถฐ์ฃผ์—‡๋‹ค.
  2. ์ตœ์†Œ๊ฐ’์œผ๋กœ ๋งž์ถฐ์ค€ํ›„ ์ดํ›„ ์ถ”๊ฐ€๋กœ ๋‹ค์‹œ ์กฐ์ •ํ•˜์—ฌ ํ…Œ์ŠคํŠธ ์˜ˆ์ •

3. ํ…Œ์ŠคํŠธ
efs ๋™์ž‘ ํ…Œ์ŠคํŠธ
  1. auto-scaling์œผ๋กœ ์ƒ์„ฑ๋œ ec2 ํ™•์ธ
    1. mount ํ™•์ธ ๋ฐ ํŒŒ์ผ ํ™•์ธ
      → ์ถ”๊ฐ€๋กœ ์•„๋ฌด ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด๋ณด๊ณ  ๊ธฐ์กด bastion์—๋„ ์ ์šฉ๋˜์—ˆ๋‚˜ ํ™•์ธ
    2. ์ •์ƒ mount ์ž‘๋™ ํ™•์ธ
cloudwatch ์ด๋ฒคํŠธ - run command ๋™์ž‘
  1. cloud watch๋ฅผ ์ด์šฉํ•ด ์ด๋ฒคํŠธ ํŒจํ„ด ์ƒ์„ฑ 
  1. ํ•ด๋‹น ์ด๋ฒคํŠธ ๋™์ž‘์‹œ - scale out์‹œ ์ด์ „์˜ ์ƒ์„ฑ๋œ efs.html์ด ๋ถˆ๋Ÿฌ์™€์ก‹๋Š”์ง€ ํ™•์ธ ํ…Œ์ŠคํŠธ
    → ์ด์ „์˜ 3 2 3 ๊ตฌ์„ฑ์„ 4 2 4๋กœ ๋ณ€๊ฒฝ์‹œ ์ธ์Šคํ„ด์Šค ์ถ”๊ฐ€ ๋˜๊ธฐ๋•Œ๋ฌธ์— ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๋ฅผ ํ™•์ธํ•  ์˜ˆ์ •
  1. ์ถ”๊ฐ€ ์ธ์Šคํ„ด์Šค ํ™•์ธ
    1. ๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ๋กœ ํ™•์ธ์‹œ
      1. ๊ธฐ๋ณธ index.html ํŽ˜์ด์ง€
      2. efs ๋กœ ๋ฐ›์•„์˜จ ๊ธฐ์กด ์ธ์Šคํ„ด์Šค๋“ค
      3. ์—๋Ÿฌ ๋ฐœ์ƒ

→ ์ตœ๊ทผ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ชป๋ฐ›์•„์™“๋‹ค ์ธ์Šคํ„ด์Šค ์‹คํ–‰ํ•˜๋ฉด์„œ auto mount๋ณด๋‹ค run command ๋™์ž‘์ด ์šฐ์„ ์‹œ ๋˜์–ด ๋ฐ›์•„์˜ค์ง€ ๋ชปํ–ˆ๋‹ค.

lambda ์ด์šฉํ•œ - run command
  1. ๋žŒ๋‹ค ์—ญํ•  ์ƒ์„ฑ → ์‚ฌ์šฉํ•  ์„œ๋น„์Šค๋“ค์— ๋Œ€ํ•ด์„œ ์—ญํ• ์„ ์ง€์ •ํ•ด์ฃผ์—ˆ๋‹ค.
    • ๋žŒ๋‹ค์˜ ๋ชจ๋“  ๊ถŒํ•œ
    • ec2๊ฐ€ ssm ๋™์ž‘์„ ํ•˜๊ธฐ ์œ„ํ•œ ๊ถŒํ•œ → ์ฝ”๋“œ๋กœ ec2๋ฅผ ์ง€์ •ํ•ด์„œ ssm์„ ๋ถ€์—ฌํ•˜๋ฏ€๋กœ
    • ssm ๋™์ž‘์„ ์œ„ํ•œ ๋ชจ๋“  ๊ถŒํ•œ
  1. ๋žŒ๋‹ค ํ•จ์ˆ˜ ์ƒ์„ฑ
  1. ์ฝ”๋“œ ์ž‘์„ฑ
    1. ๋‹จ์ผ ์ธ์Šคํ„ด์Šค run command ๋™์ž‘
    import json
    import boto3
    
    def lambda_handler(event, context):
        ssm_client = boto3.client('ssm')
        response = ssm_client.send_command(
            InstanceIds=['i-0e4c2788a45b4baf2'],
            DocumentName="AWS-RunShellScript",
            Parameters={'commands': ['sudo echo "<html><body>lambda test webserver6</body></html>" > /var/www/html/index.html']}, )
        
        return {
            'statusCode': 200
        }
    
    2. tag๋ฅผ ์ด์šฉํ•œ run command ๋™์ž‘
    import json
    import boto3
    ssm = boto3.client('ssm')
    
    def lambda_handler(event, context):
       
    
        ssmcommand = ssm.send_command(
    
            Targets = [
            {'Key': 'tag:Name', 
                 'Values': [
                        'ec2-efs']
                    }
                  ],
            DocumentName="AWS-RunShellScript",
            Parameters={'commands': 
    ['sudo cp -fv "<html><body>tag lambda runcommand22</body></html>" 
    > /efstest/index.html'],['sudo cp -fv /efstest/index.html /var/www/html/] }, )
        return{
            'status_code': 200
        }
  1. ์ด๋ฒคํŠธ ํŠธ๋ฆฌ๊ฑฐ - cloudwatch ๊ทœ์น™
    → sacle out์‹œ run command ๋™์ž‘
  1. ํ™•์ธ ๊ณผ์ •
    → ๋žŒ๋‹ค ์ฝ”๋“œ์—์„œ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ›„ ์ž‘์—…์ง„ํ–‰ํ•ด๋ณด์ž
  1. sacle out ๋™์ž‘
    → ์ •์ƒ์ ์œผ๋กœ run command ๋™์ž‘ ํ™•์ธ

 

๊ฒฐ๋ก 

๋”ฐ๋ผ์„œ, Cloudwatch๋ฅผ ์ด์šฉํ•˜์—ฌ SSM runcommand ๋ฅผ ์ ์šฉํ•˜๊ธฐ๋ณด๋‹จ Cloudwatch Event ์™€ Lambda๋ฅผ ํŠธ๋ฆฌ๊ฑฐ ์‹œํ‚ค๋Š” ๋ฐฉ์‹์ด ์šฉ์ดํ–ˆ๋‹ค,
๋˜ํ•œ, ASG ๊ตฌ์„ฑ ๋ฐฉ์‹์ค‘ Launch Template์„ ์ด์šฉํ•˜์—ฌ ๋ฒ„์ „๊ด€๋ฆฌ๋ฅผ ํ†ตํ•˜์—ฌ ์„œ๋ฒ„๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ๋ฐฉ์‹์ด ์กด์žฌ ํ•˜๋ฉฐ 
ํ•ด๋‹น Template์„ ์ธ์Šคํ„ด์Šค ์ƒˆ๋กœ๊ณ ์นจ or Warm pool ๋ฐฉ์‹์„ ์ ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•  ๊ฒƒ ์ด๋‹ค.
 

 

 

'Automation' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

SSM Automation์„ ํ†ตํ•œ ์ž๋™ํ™”  (0) 2022.08.23
image builder 2  (0) 2021.06.16
Image Builder -golden ami ์ƒ์„ฑ  (0) 2021.03.29
Lambda Ec2 start / stop Tag๊ธฐ๋ฐ˜  (0) 2021.01.20

+ Recent posts