Kanji
・クラウドエンジニア / フリーランス ・1993年生まれ ・愛媛県出身 / 東京都渋谷区在住 ・AWS歴5年 プロフィールの詳細
目次
AWS::SSM::Document
Script
# noqa
max-line-length
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)