CloudFormation validate 支持在 s3 中驗證 cloudformation 模板。
如何驗證 s3 位置中的所有檔案。這些檔案位于一個檔案夾中。
uj5u.com熱心網友回復:
您可以使用以下python腳本驗證 s3 存盤桶/檔案夾中的所有 cloudformation 模板
以下腳本生成s3 檔案夾中所有檔案的Object Url/ Public Url,然后將 url 傳遞給validate_file函式
import boto3
s3_uri="s3://BUCKET_NAME/FOLDER_1/FOLDER2/" # S3 URI of the folder you want to recursively scan, Replace this with your own S3 URI
# Split the s3 uri to extract bucket name and the file prefix
# Splitting S# 3 URI will generate an array
# Combine the appropirate elements of the array to extraxt BUCKET_NAME and PREFIX
arr=s3_uri.split('/')
bucket =arr[2]
prefix=""
for i in range(3,len(arr)-1):
prefix=prefix arr[i] "/"
s3_client = boto3.client("s3")
def validate_file(object_url): # function to validate cloudformation template
cloudformation_client = boto3.client('cloudformation')
response = cloudformation_client.validate_template(
TemplateURL=object_url
)
print(response) # print the response
def get_all_s3_files(bucket,prefix,s3_client): # generate object url of all files in the folder and pass it to validate function
response = s3_client.list_objects_v2(Bucket=bucket, Prefix=prefix) # Featch Meta-data of all the files in the folder
files = response.get("Contents")
for file in files: # Iterate through each files
file_path=file['Key']
object_url="https://" bucket ".s3.amazonaws.com/" file_path #create Object URL Manually
print("Object Url = " object_url)
if object_url.endswith(".yml"):
validate_file(object_url=object_url) # validate all files
get_all_s3_files(bucket=bucket,prefix=prefix,s3_client=s3_client)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/458813.html
標籤:Python 亚马逊-s3 亚马逊云形成 博托3 aws-cli
