Skip to main content
Added meta_templates code
Source Link

This is my my IAM policy template named "meta_templates.py"

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}}::network-interface/*",
                    "arn:aws:ec2:{{region}}::key-pair/*",
                    "arn:aws:ec2:{{region}}::security-group/*",
                    "arn:aws:ec2:{{region}}::subnet/*",
                    "arn:aws:ec2:{{region}}::volume/*",
                    "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:StartInstances",
                    "ec2:StopInstances"
                ],
                "Resource": "arn:aws:ec2:{{region}}::instance/*",
                "Condition": {
                    "ForAllValues:StringEquals": {
                        "ec2:InstanceType": "{{instance_type}}"
                    }
                }
            },
            {
                "Sid": "VisualEditor2",
                "Effect": "Allow",
                "Action": [
                    "ec2:Describe*",
                    "ec2:GetConsole*",
                    "cloudwatch:DescribeAlarms",
                    "iam:ListInstanceProfiles",
                    "cloudwatch:GetMetricStatistics",
                    "ec2:DescribeKeyPairs",
                    "ec2:CreateKeyPair"
                ],
                "Resource": "*",
                "Condition": {
                    "DateGreaterThan": {
                        "aws:CurrentTime": "{{start_time}}"
                    },
                    "DateLessThanEquals": {
                        "aws:CurrentTime": "{{end_time}}"
                    }
                }
            }
        ]
    }

I am not sure what is wrong

I am not sure what is wrong

This is my my IAM policy template named "meta_templates.py"

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}}::network-interface/*",
                    "arn:aws:ec2:{{region}}::key-pair/*",
                    "arn:aws:ec2:{{region}}::security-group/*",
                    "arn:aws:ec2:{{region}}::subnet/*",
                    "arn:aws:ec2:{{region}}::volume/*",
                    "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:StartInstances",
                    "ec2:StopInstances"
                ],
                "Resource": "arn:aws:ec2:{{region}}::instance/*",
                "Condition": {
                    "ForAllValues:StringEquals": {
                        "ec2:InstanceType": "{{instance_type}}"
                    }
                }
            },
            {
                "Sid": "VisualEditor2",
                "Effect": "Allow",
                "Action": [
                    "ec2:Describe*",
                    "ec2:GetConsole*",
                    "cloudwatch:DescribeAlarms",
                    "iam:ListInstanceProfiles",
                    "cloudwatch:GetMetricStatistics",
                    "ec2:DescribeKeyPairs",
                    "ec2:CreateKeyPair"
                ],
                "Resource": "*",
                "Condition": {
                    "DateGreaterThan": {
                        "aws:CurrentTime": "{{start_time}}"
                    },
                    "DateLessThanEquals": {
                        "aws:CurrentTime": "{{end_time}}"
                    }
                }
            }
        ]
    }

I am not sure what is wrong

added 100 characters in body
Source Link

I am working on an AWS lambda function that calls python functions and prints what my python function prints. This is my python function named "template_utils.py"

import json
import meta_templates
from jinja2 import Template
def create_aws_iam_policy_template(**kwargs):
  template_data = {}
  template_data["region"] = kwargs.get('region')
  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"
)

print(template_json)

This is my AWS lambda function:

import json
import meta_templates
from jinja2 import Template
from template_utils import create_aws_iam_policy_template 

def lambda_handler(event, context):
    template_data = {}
    region = event.get('region')
    instance_type = event.get('instance_type')
    ebs_volume_size = event.get('ebs_volume_size')
    template_data["meta_template_name"] = event.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"
)
print(template_json)

This is the error I am getting:

{
  "errorMessage": "'meta_template_name'""getattr(): attribute name must be string",
  "errorType": "KeyError""TypeError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 1112, in lambda_handler\n    meta_template_dict = getattr(meta_templates, template_data[\"meta_template_name\"])\n"
  ]
}

I am not sure what is wrong

I am working on an AWS lambda function that calls python functions and prints what my python function prints. This is my python function named "template_utils.py"

import json
import meta_templates
from jinja2 import Template
def create_aws_iam_policy_template(**kwargs):
  template_data = {}
  template_data["region"] = kwargs.get('region')
  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"
)

print(template_json)

This is my AWS lambda function:

import json
import meta_templates
from jinja2 import Template
from template_utils import create_aws_iam_policy_template 

def lambda_handler(event, context):
    template_data = {}
    region = event.get('region')
    instance_type = event.get('instance_type')
    ebs_volume_size = event.get('ebs_volume_size')
    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"
)
print(template_json)

This is the error I am getting:

{
  "errorMessage": "'meta_template_name'",
  "errorType": "KeyError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 11, in lambda_handler\n    meta_template_dict = getattr(meta_templates, template_data[\"meta_template_name\"])\n"
  ]
}

I am not sure what is wrong

I am working on an AWS lambda function that calls python functions and prints what my python function prints. This is my python function named "template_utils.py"

import json
import meta_templates
from jinja2 import Template
def create_aws_iam_policy_template(**kwargs):
  template_data = {}
  template_data["region"] = kwargs.get('region')
  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"
)

print(template_json)

This is my AWS lambda function:

import json
import meta_templates
from jinja2 import Template
from template_utils import create_aws_iam_policy_template 

def lambda_handler(event, context):
    template_data = {}
    region = event.get('region')
    instance_type = event.get('instance_type')
    ebs_volume_size = event.get('ebs_volume_size')
    template_data["meta_template_name"] = event.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"
)
print(template_json)

This is the error I am getting:

{
  "errorMessage": "getattr(): attribute name must be string",
  "errorType": "TypeError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 12, in lambda_handler\n    meta_template_dict = getattr(meta_templates, template_data[\"meta_template_name\"])\n"
  ]
}

I am not sure what is wrong

added 470 characters in body
Source Link

I am working on an AWS lambda function that calls python functions and prints what my python function prints. This is my python function named "template_utils.py"

import json
import meta_templates
from jinja2 import Template
def create_aws_iam_policy_template(**kwargs):
  template_data = {}
  template_data["region"] = kwargs.get('region')
  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"
)

print(template_json)

This is my AWS lambda function:

import json
import meta_templates
from jinja2 import Template
from template_utils import create_aws_iam_policy_template 
import json

    def lambda_handler(event, context):
        template_data = {}
        region = event.get('region')
    instance_type = event.get('instance_type')
  instance_type  ebs_volume_size = event.get('instance_type''ebs_volume_size')
    meta_template_dict = getattr(meta_templates, template_data["meta_template_name"])
 ebs_volume_size   meta_template_json = eventjson.getdumps('ebs_volume_size'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"
)
print(template_json)

This is the error I am getting:

{
  "errorMessage": "'meta_template_name'",
  "errorType": "KeyError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 11, in lambda_handler\n    meta_template_dict = getattr(meta_templates, template_data[\"meta_template_name\"])\n"
  ]
}

I am not sure what is wrong

I am working on an AWS lambda function that calls python functions and prints what my python function prints. This is my python function named "template_utils.py"

import json
import meta_templates
from jinja2 import Template
def create_aws_iam_policy_template(**kwargs):
  template_data = {}
  template_data["region"] = kwargs.get('region')
  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"
)

print(template_json)

This is my AWS lambda function:

from jinja2 import Template
from template_utils import create_aws_iam_policy_template 
import json

    def lambda_handler(event, context):
        template_data = {}
        region = event.get('region')
        instance_type = event.get('instance_type')
        ebs_volume_size = event.get('ebs_volume_size')
    print(template_json)

This is the error I am getting:

{
  "errorMessage": "'meta_template_name'",
  "errorType": "KeyError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 11, in lambda_handler\n    meta_template_dict = getattr(meta_templates, template_data[\"meta_template_name\"])\n"
  ]
}

I am not sure what is wrong

I am working on an AWS lambda function that calls python functions and prints what my python function prints. This is my python function named "template_utils.py"

import json
import meta_templates
from jinja2 import Template
def create_aws_iam_policy_template(**kwargs):
  template_data = {}
  template_data["region"] = kwargs.get('region')
  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"
)

print(template_json)

This is my AWS lambda function:

import json
import meta_templates
from jinja2 import Template
from template_utils import create_aws_iam_policy_template 

def lambda_handler(event, context):
    template_data = {}
    region = event.get('region')
    instance_type = event.get('instance_type')
    ebs_volume_size = event.get('ebs_volume_size')
    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"
)
print(template_json)

This is the error I am getting:

{
  "errorMessage": "'meta_template_name'",
  "errorType": "KeyError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 11, in lambda_handler\n    meta_template_dict = getattr(meta_templates, template_data[\"meta_template_name\"])\n"
  ]
}

I am not sure what is wrong

Source Link
Loading