ã¯ã©ã¦ãã¤ã³ãã°ã¬ã¼ã·ã§ã³2é¨æè¡3課ã®å±±ä¸ã§ãã
ä»åã¯ãAmazon Elastic Container Serviceï¼ä»¥ä¸ãECSï¼ã® ã¿ã¹ã¯ãã¼ã«ã¨ã¿ã¹ã¯å®è¡ãã¼ã«ã®éãã«ã¤ãã¦ã ç°¡åãªãµã³ãã«ã¢ããªãç¨æãã¦æ¤è¨¼ãã¦ã¿ããã¨æãã¾ãã
- (èæ¯) ã¿ã¹ã¯ãã¼ã«ã¨ã¿ã¹ã¯å®è¡ãã¼ã«ã®éãããã³ã¨æ¥ãªãã£ã
- (çµè«) ã¢ããªã®ã³ã¼ãã§AWSãªã½ã¼ã¹ã¸ã¢ã¯ã»ã¹ããå ´åãã¿ã¹ã¯ãã¼ã«ã«æ¨©éãã¤ãã
- æ¤è¨¼ãã¦ã¿ã
- ãã¿ã¼ã³1ï¼ã³ã³ããä¸ã®ã¢ããªã±ã¼ã·ã§ã³ããSecrets Managerã«ã¢ã¯ã»ã¹ãã
- ãã¿ã¼ã³2ï¼ã³ã³ããå®ç¾©ã®ç°å¢å¤æ°ã«Secrets Managerã®ã·ã¼ã¯ã¬ããæ å ±ãæ ¼ç´ãã
- ãããã«
(èæ¯) ã¿ã¹ã¯ãã¼ã«ã¨ã¿ã¹ã¯å®è¡ãã¼ã«ã®éãããã³ã¨æ¥ãªãã£ã
åãã¦ECSã触ã£ãéãã¿ã¹ã¯ãã¼ã«ã¨ã¿ã¹ã¯å®è¡ãã¼ã«ã®éãããå人çã«ãã¾ãã¡ãã³ã¨æ¥ã¾ããã§ããã å ¬å¼ããã¥ã¡ã³ãã®èª¬æã¨ãã¦ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
ã¿ã¹ã¯ãã¼ã«
Amazon ECS ã¿ã¹ã¯ã«ã¯ IAM ãã¼ã«ãé¢é£ä»ãããã¨ãã§ãã¾ããIAM ãã¼ã«ã§ä»ä¸ããã許å¯ã¯ãã¿ã¹ã¯ã§å®è¡ããã¦ããã³ã³ããã«ãã£ã¦å¼ãåãããã¾ãã
ã³ã³ããåããã¢ããªã±ã¼ã·ã§ã³ã¯ AWS API ãå¼ã³åºãå¿ è¦ãããå ´åãAWS èªè¨¼æ å ±ã§ãããã® AWS API ãªã¯ã¨ã¹ãã«ç½²åããå¿ è¦ãããã¾ãããªããã¿ã¹ã¯ã® IAM ãã¼ã«ã¯ãã¢ããªã±ã¼ã·ã§ã³ã使ç¨ããããã®èªè¨¼æ å ±ã管çããæ¦ç¥ãå©ç¨ã§ãã¾ãã
ã¿ã¹ã¯ IAM ãã¼ã« - Amazon Elastic Container Service
ã¿ã¹ã¯å®è¡ãã¼ã«
ã¿ã¹ã¯å®è¡ãã¼ã«ã¯ãã¦ã¼ã¶ã¼ã«ä»£ãã£ã¦ AWS API ã³ã¼ã«ãå®è¡ããããã®ã¢ã¯ã»ã¹è¨±å¯ã Amazon ECS ã³ã³ãã㨠Fargate ã¨ã¼ã¸ã§ã³ãã«ä»ä¸ãã¾ãã
Amazon ECS ã¿ã¹ã¯å®è¡IAM ãã¼ã« - Amazon Elastic Container Service
(çµè«) ã¢ããªã®ã³ã¼ãã§AWSãªã½ã¼ã¹ã¸ã¢ã¯ã»ã¹ããå ´åãã¿ã¹ã¯ãã¼ã«ã«æ¨©éãã¤ãã
ã¶ãã¯ãªè¨ãã¨ãã³ã³ããå ã®ã¢ããªã«ãAWSãªã½ã¼ã¹ã¸ã¢ã¯ã»ã¹ããããã®ã³ã¼ããç´æ¥æ¸ããã¦ããå ´åãã¿ã¹ã¯ãã¼ã«ã«æ¨©éãä»ããå¿ è¦ãããã¾ãã
ãªããã¿ã¹ã¯å®è¡ãã¼ã«ã«æ¨©éãã¤ããã±ã¼ã¹ã«ã¤ãã¦ãã¶ãã¯ãªè¨ãã¨ãECSã»Fargateä¸ã®è¨å®ã§AWSãªã½ã¼ã¹ã«ã¢ã¯ã»ã¹ããå ´åã§ãã ä¾ã¨ãã¦ãã³ã³ããå®ç¾©ã®è¨å®ã§ä»¥ä¸ãè¨å®ããå ´åçã該å½ãã¾ãã
ã»CloudWatchã¸ã®ãã°ä¿åãè¨å®
ã»ã³ã³ããã¤ã¡ã¼ã¸ã«ECRã®ãªãã¸ããªãæå®
ã»Secrets Managerã®ã·ã¼ã¯ã¬ããæ
å ±ãç°å¢å¤æ°ã«è¨å®
æ¤è¨¼ãã¦ã¿ã
ããã¤ã¡ã¼ã¸ãããããããããã«ã2ã¤ã®ãµã³ãã«ã¢ããªãç¨æããåãAWSãªã½ã¼ã¹ã¸ã¢ã¯ã»ã¹ãã¦ã¿ã¾ãã1ã¤ç®ã¯ã¿ã¹ã¯ãã¼ã«ã®æ¨©éã使ç¨ãããã1ã¤ã¯ã¿ã¹ã¯å®è¡ãã¼ã«ã®æ¨©éã使ç¨ãã¾ããããããããµã³ãã«ã¢ããªã®å 容ãã©ã®ããã«å¤ããã確èªãã¾ãã
æ§æ
ä»åã¯ãä¸å³ã®æ§æã§æ¤è¨¼ãè¡ãã¾ãã
AWS Secrets Managerï¼ä»¥ä¸ãSecrets Managerï¼ãããã°ã¤ã³æ å ±ãåå¾ãããã®æ å ±ã使ã£ã¦RDSã«æ¥ç¶ãã¦ã¿ã¾ãã
ãã¼ã¿ãã¼ã¹
ãã¼ã¿ãã¼ã¹ã¯ä»¥ä¸ã®æ å ±ãè¼ã£ã¦ããã ãã®ç°¡ä¾¿ãªãã®ãç¨æãã¾ãããæ¥æ¬ã®å¤§ããæ¹ããã5ã§ããï¼ãã¼ã¿ã®å 容ã«æ·±ãæå³ã¯ããã¾ããï¼
ãã¼ã¿ãã¼ã¹ã®å 容ï¼ã¯ãªãã¯ããã¨å±éããã¾ãï¼
MySQL [Lake]> SELECT * FROM Lake; +----+--------------+-----------------------+--------+ | id | name | prefectures | area | +----+--------------+-----------------------+--------+ | 1 | çµç¶æ¹ | æ»è³ç | 669.26 | | 2 | éã¶æµ¦ | è¨åç | 220 | | 3 | ãµããæ¹ | åæµ·é | 151.59 | | 4 | çªèä»£æ¹ | ç¦å³¶ç | 103.24 | | 5 | ä¸æµ· | å³¶æ ¹çã»é³¥åç | 85.74 | +----+--------------+-----------------------+--------+ 5 rows in set (0.00 sec)
â»ãã¼ã¿ãã¼ã¹ã«é¢ããè£è¶³
ãã¼ã¿ãã¼ã¹ãä¸è¨ãã¼ã¸ã«è¨è¼ã®ã¨ã³ã¸ã³ãã¼ã¸ã§ã³ã§ããå ´åãIAMãã¼ã¿ãã¼ã¹èªè¨¼ã使ãã¾ãã
IAMãã¼ã¿ãã¼ã¹èªè¨¼ã使ãã°ãããããã¹ã¯ã¼ãã使ãå¿ è¦ã¯ãªããããä¸è¨ãã¼ã¸ã«è¨è¼ã®å¶éäºé ã«å½ã¦ã¯ã¾ããªããã°ããã¡ããæ¡ç¨ããã°ãããã¨æãã¾ãããã ãä»åã¯ããã¾ã§æ¤è¨¼ãªã®ã§ãæå ã«ãã£ãMySQLãã¼ã¿ãã¼ã¹ã§Secrets Managerãå©ç¨ãã¦ãã¾ããã容赦ãã ããã
IAMããªã·ã¼
Secrets Managerã¸ã®ãGetSecretValueã権éãæã¤IAMããªã·ã¼ãç¨æãã¾ããã
AccessSecretsManagerï¼ã¯ãªãã¯ããã¨å±éããã¾ãï¼
{ "Version": "2012-10-17", "Statement": [ { "Action": [ "secretsmanager:GetSecretValue" ], "Resource": [ "arn:aws:secretsmanager:ap-northeast-1:xxxxxxxxxxxx:secret:*" ], "Effect": "Allow" } ] }
IAMãã¼ã«
以ä¸ã®3ã¤ã®IAMãã¼ã«ãç¨æãã¾ããã
ãã¼ã«å | ããªã·ã¼ | æ¦è¦ |
---|---|---|
ecsTaskRoleGetSecret | AccessSecretsManager | ã¿ã¹ã¯ãã¼ã«ç¨ãSecrets Managerã¸ã®ã¢ã¯ã»ã¹æ¨©éããã |
ecsTaskExecutionRole | AmazonECSTaskExecutionRolePolicy | ããã©ã«ãã®ã¿ã¹ã¯å®è¡ãã¼ã«ã Secrets Managerã¸ã®ã¢ã¯ã»ã¹æ¨©éãªãã |
ecsTaskExecutionGetSecret | AmazonECSTaskExecutionRolePolicy AccessSecretsManager |
ã¿ã¹ã¯å®è¡ãã¼ã«ç¨ãSecrets Managerã¸ã®ã¢ã¯ã»ã¹æ¨©éããã |
ãã¿ã¼ã³1ï¼ã³ã³ããä¸ã®ã¢ããªã±ã¼ã·ã§ã³ããSecrets Managerã«ã¢ã¯ã»ã¹ãã
ã³ã³ããã«ãããã¤ãããã¢ããªã±ã¼ã·ã§ã³ã«ãSecrets Managerããæ å ±ãåå¾ããã³ã¼ããè¨è¼ãã¾ãã ãã®å ´åãã¿ã¹ã¯ãã¼ã«ã«Secrets Managerã«ã¢ã¯ã»ã¹ããããªã·ã¼ãå¿ è¦ã§ãã
ãã¿ã¼ã³1ãµã³ãã«ã¢ããª
ãã¿ã¼ã³1ç¨ã®ãµã³ãã«ã¢ããªã¯ä»¥ä¸ã§ããä»åã¯boto3ã§Secrets Managerã«æ¥ç¶ãã¾ãã ã¾ããDBã¸ã®ã¢ã¯ã»ã¹ã«ã¯sqlalchemyã使ç¨ãã¾ãã ããã¾ã§æ¤è¨¼ç¨ã®ãµã³ãã«ãªã®ã§ãã³ã¼ããç²ãã®ã¯ã容赦ãã ããã
ãã¿ã¼ã³1ãµã³ãã«ã¢ããªï¼ã¯ãªãã¯ããã¨å±éããã¾ãï¼
# ã¢ã¸ã¥ã¼ã«ã®ã¤ã³ãã¼ã from sqlalchemy import create_engine from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Float, String, Integer import boto3 # å¤æ°ã®æ ¼ç´ secret_name = "xxxxxxxxxx" #ããã«ã·ã¼ã¯ã¬ããã®ååãè¨è¼ region_name = "ap-northeast-1" # Secrets Managerããã·ã¼ã¯ã¬ããæ å ±ãåå¾ print("Now starting get secret from Secrets Manager...") session = boto3.session.Session() client = session.client(service_name='secretsmanager', region_name=region_name) get_secret_value_response = client.get_secret_value(SecretId=secret_name) secret_string = get_secret_value_response['SecretString'] secret_dict = eval(secret_string) # Secrets Managerããåå¾ããæ å ±ãå¤æ°ã«æ ¼ç´ USER = secret_dict["username"] PASS = secret_dict["password"] DBNAME = secret_dict["dbname"] DBHOST = secret_dict["host"] # DBã«æ¥ç¶ print("Now starting access DB...") engine = create_engine(f'mysql+pymysql://{USER}:{PASS}@{DBHOST}/{DBNAME}') db_session = scoped_session(sessionmaker(autocommit=False,autoflush=False,bind=engine)) Base = declarative_base() Base.query = db_session.query_property() # ã¢ãã«ã®ä½æ class Lake(Base): __tablename__ = 'Lake' id = Column(Integer, primary_key=True) name = Column(String(64), unique=True) prefectures = Column(String(64)) area = Column(Float) def __init__(self, name=None, prefectures=None, area=None): self.name = name self.prefectures = prefectures self.area = area # importãã¦ããã¢ãã«ã«å¯¾ãã¦ããã¼ãã«ãåå¨ããªãã£ããä½æãã Base.metadata.create_all(bind=engine) # DBã®å 容ã表示 all_lakes = db_session.query(Lake).all() for lake in all_lakes: print(f'{lake.name} {lake.prefectures} {lake.area}')
æ¤è¨¼çµæï¼ã¿ã¹ã¯ãã¼ã«ã«æ¨©éãã¤ããå ´åã®ã¿æåï¼
æ¤è¨¼çµæã¨ãã¦ã¯ãã¿ã¹ã¯ãã¼ã«ã«ãecsTaskRoleGetSecretããã¤ããå ´åã®ã¿æåãã¨ãªãã¾ããã
ã¿ã¹ã¯å®è¡ãã¼ã«ã«ã¤ãã権éã¯é¢ä¿ãã¾ããã§ããã
æ¤è¨¼çµæãµããª
æ¤è¨¼ãã¿ã¼ã³ | ã¿ã¹ã¯ãã¼ã« | ã¿ã¹ã¯å®è¡ãã¼ã« | çµæ |
---|---|---|---|
ãã¿ã¼ã³â | ecsTaskRoleGetSecret | ecsTaskExecutionRole | æå |
ãã¿ã¼ã³â¡ | ecsTaskRoleGetSecret | ecsTaskExecutionGetSecret | æå |
ãã¿ã¼ã³â¢ | ãªã | ecsTaskExecutionRole | 失æ |
ãã¿ã¼ã³â£ | ãªã | ecsTaskExecutionGetSecret | 失æ |
æåã»å¤±ææããããã®ãã°ã¯ä¸å³ã§ãã
ãã¿ã¼ã³2ï¼ã³ã³ããå®ç¾©ã®ç°å¢å¤æ°ã«Secrets Managerã®ã·ã¼ã¯ã¬ããæ å ±ãæ ¼ç´ãã
ã³ã³ããå®ç¾©ã®ç°å¢å¤æ°ã«Secrets Managerã®ã·ã¼ã¯ã¬ããæ å ±ãæ ¼ç´ããæ¹æ³ã§ãã ãã®å ´åãã¿ã¹ã¯å®è¡ãã¼ã«ã«Secrets Managerã«ã¢ã¯ã»ã¹ããããªã·ã¼ãå¿ è¦ã§ãã
ã³ã³ããå®ç¾©
ãã¿ã¼ã³2ã®å ´åãECSã®ã³ã³ããå®ç¾©ã§ç°å¢å¤æ°ãè¨å®ããSecrets Managerã®å¤ãæå®ãã¾ãã
è¨å®ã®ä»æ¹ã¯ä¸è¨ãªã³ã¯ãåç §ãã ããã
ã³ã³ããã¸ã®æ©å¯ãã¼ã¿ã®åã渡ã - Amazon Elastic Container Service
ãµã³ãã«ã¢ããª
ãã¿ã¼ã³2ç¨ã®ãµã³ãã«ã¢ããªã¯ä»¥ä¸ã§ãã ã¢ããªãç´æ¥Secrets Managerã«æ¥ç¶ãããã¨ã¯ãªããã³ã³ããå®ç¾©ã§è¨å®ããç°å¢å¤æ°ãæå®ãã¾ãããã®ããããã¡ãã§ã¯boto3ã¯ä½¿ã£ã¦ãã¾ããã ããã¾ã§æ¤è¨¼ç¨ã®ãµã³ãã«ãªã®ã§ãã³ã¼ããç²ãã®ã¯ä»¥ä¸ç¥ã
ãã¿ã¼ã³2ãµã³ãã«ã¢ããªï¼ã¯ãªãã¯ããã¨å±éããã¾ãï¼
#ã¢ã¸ã¥ã¼ã«ã®ã¤ã³ãã¼ã from sqlalchemy import create_engine from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Float, String, Integer import os # ç°å¢å¤æ°ã®åå¾ USER = os.environ['USER_NAME'] PASS = os.environ['PASSWORD'] DBHOST = os.environ['DB_HOST'] DBNAME = os.environ['DB_NAME'] # DBã«æ¥ç¶ print("Now starting access DB...") engine = create_engine(f'mysql+pymysql://{USER}:{PASS}@{DBHOST}/{DBNAME}') db_session = scoped_session(sessionmaker(autocommit=False,autoflush=False,bind=engine)) Base = declarative_base() Base.query = db_session.query_property() # ã¢ãã«ã®ä½æ class Lake(Base): __tablename__ = 'Lake' id = Column(Integer, primary_key=True) name = Column(String(64), unique=True) prefectures = Column(String(64)) area = Column(Float) def __init__(self, name=None, prefectures=None, area=None): self.name = name self.prefectures = prefectures self.area = area # importãã¦ããã¢ãã«å ¨ã¦ã«å¯¾ãã¦ããã¼ãã«ãåå¨ããªãã£ããä½æãã Base.metadata.create_all(bind=engine) # DBã®å 容ã表示 all_lakes = db_session.query(Lake).all() for lake in all_lakes: print(f'{lake.name} {lake.prefectures} {lake.area}')
æ¤è¨¼çµæï¼ã¿ã¹ã¯å®è¡ãã¼ã«ã«æ¨©éãã¤ããå ´åã®ã¿æåï¼
æ¤è¨¼çµæã¨ãã¦ã¯ãã¿ã¹ã¯å®è¡ãã¼ã«ã«ãecsTaskExecutionGetSecretããã¤ããå ´åã®ã¿æåãã¨ãªãã¾ããã ã¿ã¹ã¯ãã¼ã«ã«ã¤ãã権éã¯é¢ä¿ãã¾ããã§ããã
æ¤è¨¼çµæãµããª
æ¤è¨¼ãã¿ã¼ã³ | ã¿ã¹ã¯ãã¼ã« | ã¿ã¹ã¯å®è¡ãã¼ã« | çµæ |
---|---|---|---|
ãã¿ã¼ã³â | ecsTaskRoleGetSecret | ecsTaskExecutionRole | 失æ |
ãã¿ã¼ã³â¡ | ecsTaskRoleGetSecret | ecsTaskExecutionGetSecret | æå |
ãã¿ã¼ã³â¢ | ãªã | ecsTaskExecutionRole | 失æ |
ãã¿ã¼ã³â£ | ãªã | ecsTaskExecutionGetSecret | æå |
æåã»å¤±ææããããã®ãã°ã¯ä¸å³ã§ãã
ãã¿ã¼ã³2ã®å ´åã失ææã«ã¯ãã°ã«ä½ãåºåããã¾ããã§ãããã¿ã¹ã¯ã®è©³ç´°ç»é¢ãè¦ãã¨ãã¿ã¹ã¯ãèªåã§åæ¢ãããåæ¢çç±ã«Secrets Managerã«ã¢ã¯ã»ã¹ã§ããªãã£ãæ¨ãè¨è¼ããã¦ãã¾ããã
ã¿ã¹ã¯å®è¡åã«ãã³ã³ããã¨ã¼ã¸ã§ã³ãã«ããAWSãªã½ã¼ã¹ã¸ã®ã¢ã¯ã»ã¹ã失æããããããã°ã«ä½ãåºåãããªãã£ãããã§ãã
ãããã«
ã¿ã¹ã¯ãã¼ã«ã¨ã¿ã¹ã¯å®è¡ãã¼ã«ãããããã®æ¨©éã使ã£ã¦Secrets Managerã®æ å ±ãåå¾ãã¦ã¿ã¾ããã å®éã«ãµã³ãã«ã¢ããªãç¨æãã¦ãåããªã½ã¼ã¹ã¸ã¢ã¯ã»ã¹ãã¦ã¿ããã¨ã§ã両è ã®éããã¤ã¡ã¼ã¸ãããããªãã¾ããã
åããããªãæ©ã¿ãæã£ãæ¹ã«ããã®è¨äºãå°ãã§ãåèã«ãªãã°å¹¸ãã§ãã
å±±ä¸ ç¥æ¨¹(å·çè¨äºã®ä¸è¦§)
2021å¹´11æä¸éå ¥ç¤¾ãåè·ã§ã¯æ ã·ã¹ã¨ãã¦ç¤¾å ãããã¯ã¼ã¯ã®æ´æ¹ãéç¨ã«æºãã£ã¦ãã¾ããã 2023 Japan AWS All Certifications Engineersã