【发布时间】:2021-04-18 08:07:25
【问题描述】:
我正在编写一个 python 脚本,它将使用 lambda 函数在 dynamodb 表中存储 JSON 属性,例如 ("region","ebs_volume_size","instance_type") 这是我的 lambda 函数,它从我的 python 函数中获取策略的输入
import boto3
import time
import json
import meta_templates
from jinja2 import Template
from template_utils import create_aws_iam_policy_template
dynamodb = boto3.resource('dynamodb')
lambd = boto3.client('lambda')
def lambda_handler(event, context):
template_json = create_aws_iam_policy_template(**event)
return template_json
table =dynamodb.create_table(
TableName='GoodTable',
AttributeDefinitions=[
{
"AttributeName": "Content",
"AttributeType": "S"
}
],
KeySchema=[
{
"AttributeName": "Content",
"KeyType": "HASH"
}
],
ProvisionedThroughput={
"ReadCapacityUnits": 1,
"WriteCapacityUnits": 1
}
)
time.sleep(20)
table = dynamodb.Table('GoodTable')
response = table.put_item(
Item= {
'Content': 'Volume Size',
'Details': kwargs.get('ebs_volume_size'),
}
)
response = table.put_item(
Item= {
'Content': 'Instance Type',
'Details': kwargs.get('instance_type'),
}
)
response = table.put_item(
Item= {
'Content': 'Region',
'Details': kwargs.get('region'),
}
)
这是我的python函数“template_utils.py”:
import json
import meta_templates
from jinja2 import Template
start_time_1 = input("What's the start time")
end_time1 = input("What's the end time")
def create_aws_iam_policy_template(**kwargs):
template_data = {}
template_data["region"] = kwargs.get('region')
template_data["start_time"] = kwargs.get('end_time')
template_data["end_time"] = kwargs.get('start_time')
template_data["instance_types"] = kwargs.get('instance_type')
template_data["ebs_volume_size"] = kwargs.get('ebs_volume_size')
template_data["meta_template_name"] = kwargs.get('meta_template_name')
meta_template_dict = getattr(meta_templates, template_data["meta_template_name"])
meta_template_json = json.dumps(meta_template_dict)
template_json = Template(meta_template_json).render(template_data)
return template_json
template_json = create_aws_iam_policy_template(
region="us-east-2",
instance_type="t2.micro",
ebs_volume_size="20",
meta_template_name="ec2_policy_meta_template",
start_time = start_time_1,
end_time = end_time1
)
print(template_json)
This is my IAM policy:
ec2_policy_meta_template = {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "ec2:RunInstances",
"Resource": [
"arn:aws:ec2:{{region}}::instance/*",
"arn:aws:ec2:{{region}}::image/ami-*"
],
"Condition": {
"ForAllValues:NumericLessThanEquals": {
"ec2:VolumeSize": "{{ebs_volume_size}}"
},
"ForAllValues:StringEquals": {
"ec2:InstanceType": "{{instance_type}}"
}
}
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": [
"ec2:TerminateInstances",
"ec2:StopInstances"
],
"Resource": "arn:aws:ec2:{{region}}::instance/*",
"Condition": {
"ForAllValues:StringEquals": {
"ec2:InstanceType": "{{instance_type}}"
}
}
},
{
"Sid": "VisualEditor2",
"Effect": "Allow",
"Action": [
"ec2:Describe*",
"ec2:GetConsole*",
"ec2:CreateKeyPair"
],
"Resource": "*",
"Condition": {
"DateGreaterThan": {
"aws:CurrentTime": "{{start_time}}"
},
"DateLessThanEquals": {
"aws:CurrentTime": "{{end_time}}"
}
}
}
]
}
我不想使用“f-strings”而只是使用 python 函数中的值,我收到一个错误“未定义的变量'kwargs'”。
【问题讨论】:
-
您的 lambda 缩进错误。
dynamodb.create_table永远不会运行。ebs_volume_size应该来自哪里?它是 lambda 函数的输入吗? -
“ebs_volume_size”将来自“template_json”
标签: python python-3.x amazon-web-services aws-lambda boto3