Kanji
・클라우드 엔지니어 / 프리랜서 ・1993년생 ・에히메현 출신 / 도쿄도 시부야구 거주 ・AWS 경력 5년 프로필 상세
목차
AWS::SSM::Document
Script
os
argparse
boto3
json
logging
time
datetime
이 문서의 Python 스크립트는 PEP8을 따릅니다.
’flake8’로 문법 검사를 하고, ’black’으로 자동 포맷팅을 합니다.
처리 요구사항으로 인해 검사를 무시해야 하는 코드에는 ’#noqa’가 추가됩니다.
최대 줄 길이( max-line-length )는 200자로 설정됩니다.
max-line-length
검증에 사용되는 Python 런타임 버전은 3.12.1입니다.
ruamel.yaml
package-ssm-document.py
aws cloudformation package
import argparse import os from ruamel.yaml import YAML from ruamel.yaml.scalarstring import LiteralScalarString yaml = YAML() yaml.default_flow_style = False yaml.sort_keys = False def replace_script_with_inline_code(input_file, output_file): base_dir = os.path.dirname(input_file) with open(input_file, "r") as file: yaml_content = yaml.load(file) def process_main_steps(main_steps): for step in main_steps: if step.get("action") == "aws:executeScript" and "Script" in step["inputs"]: script_path = step["inputs"]["Script"] full_script_path = os.path.normpath(os.path.join(base_dir, script_path)) if os.path.exists(full_script_path): with open(full_script_path, "r") as script_file: script_content = script_file.read() step["inputs"]["Script"] = apply_literal_block_style(script_content) else: print(f"Warning: Script file not found at {full_script_path}") elif step.get("action") == "aws:loop" and "Steps" in step["inputs"]: process_main_steps(step["inputs"]["Steps"]) if "nextStep" in step: process_main_steps(step.get("nextStep", [])) def apply_literal_block_style(script_content): return LiteralScalarString(script_content) for resource in yaml_content.get("Resources", {}).values(): if resource.get("Type") == "AWS::SSM::Document": content = resource.get("Properties", {}).get("Content", {}) if "mainSteps" in content: process_main_steps(content["mainSteps"]) output_dir = os.path.dirname(output_file) if output_dir and not os.path.exists(output_dir): os.makedirs(output_dir) with open(output_file, "w") as file: yaml.dump(yaml_content, file) if __name__ == "__main__": parser = argparse.ArgumentParser(description="Transform parameter overrides in a YAML file.") parser.add_argument("--input-file", help="Path to the input YAML file.", default="template.original.yml") parser.add_argument("--output-file", help="Path to the output YAML file.", default="template.yml") args = parser.parse_args() replace_script_with_inline_code(args.input_file, args.output_file)
template.original.yml
template.yml
python package-ssm-document.py \ --input-file tests/template1.original.yml \ --output-file .alcache/template.yml
src/sample1.py
AWSTemplateFormatVersion: 2010-09-09 Description: template.yml Resources: TestRunbook: Type: AWS::SSM::Document Properties: Name: Sample-TestRunbook DocumentType: Automation DocumentFormat: YAML UpdateMethod: NewVersion Content: schemaVersion: '0.3' mainSteps: - name: SampleStep action: aws:executeScript isEnd: true inputs: Runtime: python3.11 Handler: main Script: src/sample1.py
AWSTemplateFormatVersion: 2010-09-09 Description: template.yml Resources: TestRunbook: Type: AWS::SSM::Document Properties: Name: Sample-TestRunbook DocumentType: Automation DocumentFormat: YAML UpdateMethod: NewVersion Content: schemaVersion: '0.3' mainSteps: - name: SampleStep action: aws:executeScript isEnd: true inputs: Runtime: python3.11 Handler: main Script: | def main(event, context): print("[sample1] Hello, World!") if __name__ == "__main__": main(None, None)