Reputation: 77
I am working on a python script that will store JSON attributes like ("region","ebs_volume_size","instance_type")inside a dynamodb table using a lambda function This my lambda function which is taking an input from my python function for the policy
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'),
}
)
This my python function "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}}"
}
}
}
]
}
I don't want to use "f-strings" rather just the values from the python function, I am getting an error "undefined variable'kwargs'".
Upvotes: 0
Views: 499
Reputation: 238209
The indentation and kwargs should be fixed in the corrected version of the code below. Also make sure that you provide correct permissions in lambda execution role so that it has permissions to access dynamodb.
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)
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': template_json.get('ebs_volume_size'),
}
)
response = table.put_item(
Item= {
'Content': 'Instance Type',
'Details': template_json.get('instance_type'),
}
)
response = table.put_item(
Item= {
'Content': 'Region',
'Details': template_json.get('region'),
}
)
Upvotes: 1