create environment
name : my-cloud9
default๋ก ์ ์ง
cost-saving setting ; ์๋์ผ๋ก ์ต๋์ ์ ๋ชจ๋ ์ ํ (ec2) - Network settings (advanced)
vpc default๋ก ํด๋ ๋จ. ๊ทผ๋ฐ ๊ทธ๋ฅ ์ค์ ํ์.
+ํ๊ทธ ๋ฃ๊ธฐ
ํ์ธ ํ create
IDE ํ๊ฒฝ ์์ฑ ์๋ฃ
my-cloud9์ด ํํด๋(environment).
hello.py ํ์ผ ์์ฑ (my-cloud ํด๋ ์ฐํด๋ฆญ - New File)
import sys
print('Hello, World!')
print('The sum of 2 and 3 is 5.')
sum = int(sys.argv[1]) + int(sys.argv[2])
print('The sum of {0} and {1} is {2}.'.format(sys.argv[1], sys.argv[2], sum))
ํ์ผ์ ์ ๋ช ๋ น์ด ์ ๋ ฅ
run
command ์ด์ฉ
boto3๋ฅผ ์ค์นํด์ผํจ (SDK)
curl -O https://bootstrap.pypa.io/get-pip.py
python get-pip.py
python -m pip --version
rm get-pip.py
python -m pip install boto3
python -m pip show boto3
์ ๋ช
๋ น์ด bashํญ์์ ์
๋ ฅํ์ฌ ์ค์น
s3.pyํ์ผ ํญ์์ ์์ฑํ์ฌ ์๋ ๋ช ๋ น์ด ์ ๋ ฅ
import sys
import boto3
from botocore.exceptions import ClientError
def get_s3(region=None):
"""
Get a Boto 3 Amazon S3 resource with a specific AWS Region or with your
default AWS Region.
"""
return boto3.resource('s3', region_name=region) if region else boto3.resource('s3')
def list_my_buckets(s3):
print('Buckets:\n\t', *[b.name for b in s3.buckets.all()], sep="\n\t")
def create_and_delete_my_bucket(bucket_name, region, keep_bucket):
s3 = get_s3(region)
list_my_buckets(s3)
try:
print('\nCreating new bucket:', bucket_name)
bucket = s3.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={
'LocationConstraint': region
}
)
except ClientError as e:
print(e)
sys.exit('Exiting the script because bucket creation failed.')
bucket.wait_until_exists()
list_my_buckets(s3)
if not keep_bucket:
print('\nDeleting bucket:', bucket.name)
bucket.delete()
bucket.wait_until_not_exists()
list_my_buckets(s3)
else:
print('\nKeeping bucket:', bucket.name)
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('bucket_name', help='The name of the bucket to create.')
parser.add_argument('region', help='The region in which to create your bucket.')
parser.add_argument('--keep_bucket', help='Keeps the created bucket. When not '
'specified, the bucket is deleted '
'at the end of the demo.',
action='store_true')
args = parser.parse_args()
create_and_delete_my_bucket(args.bucket_name, args.region, args.keep_bucket)
if __name__ == '__main__':
main()
์ปค๋งจ๋์ฐฝ์ ์๋ ๋ช ๋ น์ด ์ ๋ ฅ.
s3.py s3.lovemj.shop ap-northeast-2 --keep_bucket
ํ๊ทธ skipํ๊ณ ์์ฑ, .csvํ์ผ ๋ค์ด๋ก๋ .
dev-user ํด๋ฆญํด์ ์ฌ์ฉ์ ์ง์
- ๋ณด์ ์๊ฒฉ ์ฆ๋ช
- ํ๋จ์ผ๋ก ๋ด๋ ค์ AWS CodeCommit์ ๋ํ HTTPS Git ์๊ฒฉ ์ฆ๋ช
์์ ์๊ฒฉ์ฆ๋ช
์์ฑํด๋๊ธฐ. + ์๊ฒฉ์ฆ๋ช
๋ค์ด๋ก๋
->github์ด์ฉํ ๋ ๋งค๋ฒ ๋ก๊ทธ์ธํ์ง ์์๋ ๋๋๋ก.
โ๏ธ๋ฆฌํฌ์งํ ๋ฆฌ ์์ฑ - ์ด๋ฆ : my-repo -์์ฑ
โ๏ธcloud9 bash์์ ๋ฒ์ ํ์ธ
โ๏ธcodecommit https ๋ณต์ ํด์ git clone ์งํ
git clone https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/my-repo
-> ์ด๋ฏธ ์๊ฒฉ์ฆ๋ช
๋์ด์์ด์ aws configure ๋ช
๋ น์ด ํ์ ์์ ์ถํ์ ๋ค๋ฅธ ec2์์ ํ๊ฑฐ๋ ํ๋ฉด aws configure ํด์ ์๊ฒฉ์ฆ๋ช
ํด์ฃผ๋ฉด ๋จ. ๊ธฐํ - aws git ์๊ฒฉ์ฆ๋ช
์ฐธ๊ณ
->migration ์ค์ตํ๊ธฐ ์ํด ์์ฑ๋ my-repo ์ญ์
โ๏ธ github๋ฆฌํฌ์งํ ๋ฆฌ migration
git clone ํ๊ธฐ ์ํด github ์ฃผ์ ๋ณต์ฌ
migrationํ๊ธฐ ์ํด codecommit ๋ฆฌํฌ์งํ ๋ฆฌ ์ฃผ์ ๋ณต์ฌํ์ฌ ๋ง์ด๊ทธ๋์ด์ ์งํ
ec2-user:~/environment $ mkdir git-migration
ec2-user:~/environment $ git clone --mirror https://github.com/mangjini/hello-world.git git-migration
ec2-user:~/environment/git-migration (BARE:master) $ git push -uf https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/my-repo --all
๋ง์ด๊ทธ๋ ์ด์
์๋ฃ ํ์ธ
ec2-user:~/environment $ npm install vue
ec2-user:~/environment $ npm install --global vue-cli
codebuild-repo์ฃผ์ ๋ณต์ฌํด์ git clone.
ec2-user:~/environment $ git clone https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/codebuild-repo
vue ์นํฉ ์ค์น
ec2-user:~/environment $ vue init webpack codebuild-repo
ec2-user:~/environment $ cd codebuild-repo/
ec2-user:~/environment/codebuild-repo (master) $ ls
build config index.html node_modules package.json package-lock.json README.md src static test
ec2-user:~/environment/codebuild-repo (master) $
ec2-user:~/environment/codebuild-repo (master) $ git add.
git: 'add.' is not a git command. See 'git --help'.
The most similar command is
add
ec2-user:~/environment/codebuild-repo (master) $ git add .
ec2-user:~/environment/codebuild-repo (master) $ git status
ec2-user:~/environment/codebuild-repo (master) $ git commit -m "uploading new file"
ec2-user:~/environment/codebuild-repo (master) $ git push
โ๏ธs3 ๋ฒํท- ์์ฑ -์ ์ ์น ์ฌ์ดํธ ํธ์คํ ํธ์ง
๋ณ๊ฒฝ์ฌํญ ์ ์ฅ ํด๋ฆญ
โ๏ธ๊ถํ - ํผ๋ธ๋ฆญ ์ก์ธ์ค ํ์ฉ ํ์ธ
โ๏ธ ๊ถํ - ๋ฒํท์ ์ฑ
ํธ์ง - ์ ์ฑ
์์ฑ๊ธฐ
โ๏ธ
ARN : arn:aws:s3:::s3.lovemj.shop
{
"Id": "Policy1660876142326",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1660876078119",
"Action": [
"s3:GetObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::s3.lovemj.shop/*",
"Principal": "*"
}
]
}
arn์ ๋ฒํท์ด๋ฆ ์์ /* ๊ผญํด์ฃผ๊ธฐ!
ํ๋ก์ ํธ ๋น๋ - ๋น๋ ํ๋ก์ ํธ ์์ฑ
๋๋จธ์ง default - ๋น๋ ํ๋ก์ ํธ ์์ฑ
codebuild ๊ฒ์ - s3 role ํด๋ฆญ
๊ถํ์ ์ฑ
- ๊ถํ ์ถ๊ฐ - ์ ์ฑ
์ฐ๊ฒฐ - s3 ๊ฒ์ - s3FullAccess - ํ๋จ์ ์ ์ฑ
์ฐ๊ฒฐ ํด๋ฆญ
๊ฒฝ๋ก์ ๋ง๊ฒ buildspec.yml ์์ฑ
version: 0.2
phases:
install:
runtime-versions:
nodejs: 16
commands:
- npm i npm@latest -g
pre_build:
commands:
- npm install
build:
commands:
- npm run build
post_build:
commands:
- aws s3 sync ./dist s3://s3.lovemj.shop
git add .
git commit -m "codebuild test commit"
git push
codebuild์์ ๋น๋ ์์
s3์์ฑ์์ ๋ฒํท ์น์ฌ์ดํธ ์๋ํฌ์ธํธ๋ก ์ง์
route53์์ ๋๋ฉ์ธ ์ค์ -> ๋ฒํท ์ด๋ฆ๊ณผ ๋๋ฉ์ธ์ด ๊ฐ์์ผํจ.
cli ํ๊ฒฝ์์๋ ์๋์ ๊ฐ์ด ์ง์ ํ์ฌ ์์ ๊ฐ๋ฅ
ec2-user:~/environment/codebuild-repo/src/components (master) $ vi HelloWorld.vue
โ๏ธ git pushํด์ฃผ๊ธฐ
ec2-user:~/environment/codebuild-repo (master) $ git add .
ec2-user:~/environment/codebuild-repo (master) $ git status
ec2-user:~/environment/codebuild-repo (master) $ git commit -m "Hello mangji add"
ec2-user:~/environment/codebuild-repo (master) $ git push
โ๏ธcode build์์ ๋น๋์์ ํด๋ฆญ
์์ ํ์ธ
iam - ์ญํ ๋ง๋ค๊ธฐ -
-> ์ญํ ์์ฑ
โ๏ธ์ญํ ์ ์ ๋ขฐ ๊ด๊ณ ํธ์ง
codedeployroleํด๋ฆญ - ์ ๋ขฐ ๊ด๊ณ - ์ ๋ขฐ ์ ์ฑ
ํธ์ง -
๊ธฐ์กด json๋ฌธ ์ญ์ ํ๊ณ ์๋ ๋ด์ฉ ๋ถ์ฌ๋ฃ๊ธฐ
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": [
"codedeploy.us-east-2.amazonaws.com",
"codedeploy.us-east-1.amazonaws.com",
"codedeploy.us-west-1.amazonaws.com",
"codedeploy.us-west-2.amazonaws.com",
"codedeploy.eu-west-3.amazonaws.com",
"codedeploy.ca-central-1.amazonaws.com",
"codedeploy.eu-west-1.amazonaws.com",
"codedeploy.eu-west-2.amazonaws.com",
"codedeploy.eu-central-1.amazonaws.com",
"codedeploy.ap-east-1.amazonaws.com",
"codedeploy.ap-northeast-1.amazonaws.com",
"codedeploy.ap-northeast-2.amazonaws.com",
"codedeploy.ap-southeast-1.amazonaws.com",
"codedeploy.ap-southeast-2.amazonaws.com",
"codedeploy.ap-south-1.amazonaws.com",
"codedeploy.sa-east-1.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}
์ ์ฑ ์ ๋ฐ์ดํธ ํด๋ฆญ
iam - ์ ์ฑ - ์ ์ฑ ์์ฑ - JSON - ๊ธฐ์กด ๋ด์ฉ ์ง์ฐ๊ณ ์๋ ๋ด์ฉ ๋ถ์ฌ๋ฃ๊ธฐ
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:Get*",
"s3:List*"
],
"Resource": [
"arn:aws:s3:::replace-with-your-s3-bucket-name/*",
"arn:aws:s3:::aws-codedeploy-us-east-2/*",
"arn:aws:s3:::aws-codedeploy-us-east-1/*",
"arn:aws:s3:::aws-codedeploy-us-west-1/*",
"arn:aws:s3:::aws-codedeploy-us-west-2/*",
"arn:aws:s3:::aws-codedeploy-ca-central-1/*",
"arn:aws:s3:::aws-codedeploy-eu-west-1/*",
"arn:aws:s3:::aws-codedeploy-eu-west-2/*",
"arn:aws:s3:::aws-codedeploy-eu-west-3/*",
"arn:aws:s3:::aws-codedeploy-eu-central-1/*",
"arn:aws:s3:::aws-codedeploy-ap-east-1/*",
"arn:aws:s3:::aws-codedeploy-ap-northeast-1/*",
"arn:aws:s3:::aws-codedeploy-ap-northeast-2/*",
"arn:aws:s3:::aws-codedeploy-ap-southeast-1/*",
"arn:aws:s3:::aws-codedeploy-ap-southeast-2/*",
"arn:aws:s3:::aws-codedeploy-ap-south-1/*",
"arn:aws:s3:::aws-codedeploy-sa-east-1/*"
]
}
]
}
๋ค์ : ํ๊ทธ - ํ๊ทธ ์๋ต - ๋ค์: ๊ฒํ
์ด๋ฆ : codedeploy-ec2 , ์ ์ฑ ์์ฑ ๋ฒํผ ํด๋ฆญ
iam- ์ญํ - ์ญํ ๋ง๋ค๊ธฐ
-๋ค์ - codedeploy-ec2 ์ ํ , s3 ๊ฒ์ํด์ AmazonS3FullAccess ์ ํ - ๋ค์ - ์ญํ ์ด๋ฆ : codedeploy-ec2-role
์ญํ ์์ฑ ํด๋ฆญ
์์ ํ
ํ๋ฆฟ์ผ๋ก ํด๋ ๋์ง๋ง ์์๊ตฌ์ฑ์ผ๋ก ํด๋ณด์.
โ๏ธec2- ์์๊ตฌ์ฑ- ์์ ๊ตฌ์ฑ์์ฑ - ์ด๋ฆ : my-lc - AMI : ami-01711d925a1e4cc3a - ์ธ์คํด์ค ์ ํ : t2.micro - ์ธ์คํด์ค ํ๋กํ์ ์ญํ ํ ๋น
โ๏ธ๋ณด์๊ทธ๋ฃน ์ ํ
โ๏ธ ํค ํ์ด ์ ํ, ์ฒดํฌ๋ฐ์ค ์ฒดํฌ
โ๏ธ ๋ค์ ์๋ก ์ฌ๋ผ๊ฐ์ ,์ถ๊ฐ๊ตฌ์ฑ - ๊ณ ๊ธ์ธ๋ถ ์ ๋ณด ํผ์น๊ธฐ ; ์ฌ์ฉ์ ๋ฐ์ดํฐ :
#!/bin/bash
yum update -y
yum install -y ruby
curl -O https://aws-codedeploy-ap-northeast-2.s3.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
->codedeploy ์์ด์ ํธ ์ค์น ํ์.
(ec2๊ฐ ์ฝ๋๋ํ๋ก์ด๋ก๋ถํฐ ์ค๋ ๋ฐ์ดํฐ๋ฅผ ๋ฐ๊ธฐ ์ํด.)
-> ์์ ๊ตฌ์ฑ ์์ฑ ํด๋ฆญ
โ๏ธ๋ค์ ์ผ์ชฝ ec2 ๋ฉ๋ด์์ - auto scaling ๊ทธ๋ฃน - auto scaling ๊ทธ๋ฃน ์์ฑ
โ๏ธ์ด๋ฆ : my-asg, ํ๋จ์ ์์ ๊ตฌ์ฑ์ผ๋ก ์ ํ ํด๋ฆญ , ๋ง๋ค์ด๋ ์์ ๊ตฌ์ฑ ์ ํ (my-lc)
โ๏ธ๋คํธ์ํฌ MY-VPC , ํผ๋ธ๋ฆญ ์๋ธ๋ท ์ ํ (A,C)
โ๏ธ ์ ๋ก๋ ๋ฐธ๋ฐ์์ ์ฐ๊ฒฐ - ์ ํ : ALB - ๋ก๋ ๋ฐธ๋ฐ์ ์ด๋ฆ : my-asg-alb - ์ฒด๊ณ ; internet-facing (์ธํฐ๋ท ๋ฐฐํฌ ์ฉ์ด๋๊น) - ์๊น ์ ํํด๋ ๋คํธ์ํฌ ํ์ธ - ๋ฆฌ์ค๋ ๋ฐ ๋ผ์ฐํ
- ๋์๊ทธ๋ฃน์์ฑ ; ์ด๋ฆ : my-asg-tg
๋๋จธ์ง default, ๋ค์
์ํํ์ธ elb๋ก ํ๋ฉด ๋ณด๋ค์ ํํ๊ณ ์์ธํ ์ํํ์ธ ๊ฐ๋ฅ. ํ์ง๋ง ์ง๊ธ ์ฐ๋ฆฌ deploy์ผ์ด์ค์์๋ ๋ง์ง ์์์ ec2๋ก ๊ทธ๋๋ก ๋๊ฒ ์. (deploy ๋ฐฐํฌ ํ
์ค์ ์ํํ์ธ๋๋ฉด ๊ณค๋)
โ๏ธ ๊ทธ๋ฃนํฌ๊ธฐ ์ค์ - ๋ค์
โ๏ธ ๋ค skipํ๊ณ , auto scaling ๊ทธ๋ฃน ์์ฑ ํด๋ฆญ
ํฌ๊ธฐ์กฐ์ ์ ์ฑ
๋์ค์
โ๏ธ ์คํ ์ค์ผ์ผ๋ง ์ ํํด์ ์ ๋ณด ๋ณด๊ธฐ
ํด๋ ์์ฑ ; codedeploy
ํ์ผ ์์ฑ ;index.html
index.html ํ์ผ ์์ ๋ด์ฉ ๋ฃ์ด์ฃผ๊ธฐ
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sample Deployment</title>
<style>
body {
color: #ffffff;
background-color: #0188cc;
font-family: Arial, sans-serif;
font-size: 14px;
}
h1 {
font-size: 500%;
font-weight: normal;
margin-bottom: 0;
}
h2 {
font-size: 200%;
font-weight: normal;
margin-bottom: 0;
}
</style>
</head>
<body>
<div align="center">
<h1>Congratulations</h1>
<h2>This application was deployed using AWS CodeDeploy.</h2>
<p>For next steps, read the <a href="http://aws.amazon.com/documentation/codedeploy">AWS CodeDeploy Documentation</a>.</p>
</div>
</body>
</html>
ํ์ผ์์ฑ ; appspec.yml
์๋ ๋ด์ฉ ๋ฃ์ด์ฃผ๊ธฐ
version: 0.0
os: linux
files:
- source: /index.html
destination: /var/www/html/
hooks:
BeforeInstall:
- location: scripts/install_dependencies
timeout: 300
runas: root
- location: scripts/start_server
timeout: 300
runas: root
ApplicationStop:
- location: scripts/stop_server
timeout: 300
runas: root
ํด๋์์ฑ ;scripts , ์์install_dependencies,start_server,stop_server ํ์ผ ์์ฑ
๋ด์ฉ ;
vi install_dependencies
#!/bin/bash
yum install -y httpd
vi start_server
#!/bin/bash
systemctl start httpd
vi stop_server
#!/bin/bash
isExistApp = `pgrep httpd`
if [[ -n $isExistApp ]]; then
systemctl stop httpd
fi
๐ข ํ์ฅ์๋ช ์๋ ํ์ผ์์ฑ์ GUI์์ ์์ฑ X CLI์์์์ฑํด์ฃผ์ธ์. ์ธ์ฝ๋ฉ์ ๋ฌธ์ ์๊น.
โ๏ธํ์ผ ์์ถ ๋ฐ s3๋ก ๋ณด๋ด๊ธฐ , ํ์ธ
# zip -r codedeploy-sample.zip *
# aws s3 cp codedeploy-sample.zip s3://s3.lovemj.shop
ec2-user:~/environment/codedeploy $ aws s3 ls s3://s3.lovemj.shop
PRE static/
2022-08-19 07:27:06 1676 codedeploy-sample.zip
2022-08-19 05:25:42 516 index.html
code deploy - ์ ํ๋ฆฌ์ผ์ด์
- ์ ํ๋ฆฌ์ผ์ด์
์์ฑ
๋ฐฐํฌ๊ทธ๋ฃน ์์ฑ - ์ด๋ฆ : my-dg - ์๋น์ค ์ญํ : codedeployrole - ํ๊ฒฝ ๊ตฌ์ฑ : Amazon ec2 ์คํ ์ค์ผ์ผ๋ง ๊ทธ๋ฃน ;๋ง๋ค์ด๋ my-asg ์ ํ
๋ก๋๋ฐธ๋ฐ์ - ๋ก๋๋ฐธ๋ฐ์ฑ ํ์ฑํ ํด์ - ๋ฐฐํฌ๊ทธ๋ฃน์์ฑ
๋ฐฐํฌ ์์ฑ - ์๋์ ๊ฐ์ด ์ ๋ ฅ , ๋ฐฐํฌ๋ง๋ค๊ธฐ ํด๋ฆญ
-> ๋ฐฐํฌ ์๋ฃ
์นํ์ด์ง ์ ๊ทผ ํ์ธ
์ฃผ์ ๊ฐ์ํ
ec2-user:~/environment/codedeploy $ rm -rf codedeploy-sample.zip
ec2-user:~/environment/codedeploy $ zip -r codedeploy-sample.zip *
adding: appspec.yml (deflated 53%)
adding: index.html (deflated 47%)
adding: scripts/ (stored 0%)
adding: scripts/install_dependencies (stored 0%)
adding: scripts/start_server (stored 0%)
adding: scripts/stop_server (deflated 15%)
ec2-user:~/environment/codedeploy $ ll
total 12
-rw-r--r-- 1 ec2-user ec2-user 357 Aug 19 07:09 appspec.yml
-rw-rw-r-- 1 ec2-user ec2-user 1685 Aug 19 08:08 codedeploy-sample.zip
-rw-r--r-- 1 ec2-user ec2-user 732 Aug 19 08:06 index.html
drwxr-xr-x 2 ec2-user ec2-user 73 Aug 19 07:19 scripts
ec2-user:~/environment/codedeploy $ aws s3 cp codedeploy-sample.zip s3://s3.lovemj.shop
upload: ./codedeploy-sample.zip to s3://s3.lovemj.shop/codedeploy-sample.zip
ec2-user:~/environment/codedeploy $ aws s3 ls s3://s3.lovemj.shop
PRE static/
2022-08-19 08:10:14 1685 codedeploy-sample.zip
2022-08-19 05:25:42 516 index.html
๋ฐฐํฌ์ฌ์๋ ํด๋ฆญ
์นํ์ด์ง์์ ๋ฐ๋ ๊ฒ ํ์ธ
์ฌ์ฉ์ - ๋ง๋ค์ด๋๊ฒ ์ ํ - ๊ถํ์ถ๊ฐ
๊ธฐ์กด์ ์ฑ ์ง์ ์ฐ๊ฒฐ-AWSCodePipeline_FullAccess ์ ํ - ๋ค์ (์๋ฃ)
๐ โ๏ธ-โ๏ธ๐ขโญ๏ธ๐
aws configure
git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.useHttpPath true