简体   繁体   English

在没有默认 vpc 的情况下使用 Lambda 将自定义 cidr 添加到入口安全组

[英]Adding custom cidr to ingress security group using Lambda without default vpc

First of all I have been searching stackflow and the inte.net for this but I didn't find exactly where the issue is.首先,我一直在为此搜索 stackflow 和 inte.net,但我没有找到问题的确切位置。

Basically I am trying to add custom cidr ips to a security group via lambda function. I have given all the appropriate permissions (as far as i can tell) [REMOVED]and also tried attaching the vpc (which is non-default) to the lambda function to access the security group[REMOVED] .基本上我正在尝试通过 lambda function 将自定义 cidr ips 添加到安全组。我已经提供了所有适当的权限(据我所知) [REMOVED]and also tried attaching the vpc (which is non-default) to the lambda function to access the security group[REMOVED]

But I am getting "An error occurred (VPCIdNotSpecified) when calling the AuthorizeSecurityGroupIngress operation: No default VPC for this user"但我收到"An error occurred (VPCIdNotSpecified) when calling the AuthorizeSecurityGroupIngress operation: No default VPC for this user"

Policy:政策:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "ec2:RevokeSecurityGroupIngress",
                "ec2:CreateNetworkInterface",
                "ec2:AuthorizeSecurityGroupIngress",
                "ec2:DescribeNetworkInterfaces",
                "ec2:DescribeVpcs",
                "ec2:DeleteNetworkInterface",
                "ec2:DescribeSubnets",
                "ec2:DescribeSecurityGroups"
            ],
            "Resource": "*"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogStream",
                "wafv2:GetIPSet",
                "logs:CreateLogGroup",
                "wafv2:UpdateIPSet"
            ],
            "Resource": [
                "arn:aws:logs:us-west-2:xxxx:log-group:xxx:log-stream:*",
                "arn:aws:wafv2:us-west-2:xxx:*/ipset/*/*"
            ]
        }
    ]
}

Lambda function: Lambda function:

#!/usr/bin/python3.9
import boto3
ec2 = boto3.client('ec2')
def lambda_handler(event, context):
    response = ec2.authorize_security_group_ingress(
    GroupId='sg-xxxxxxx'
    IpPermissions=[
        { 
            'FromPort': 443,
            'IpProtocol': 'tcp',
            'IpRanges': [
                {
                    'CidrIp': '1x.1x.x.1x/32',
                    'Description': 'adding test cidr using lambda'
                },
            ],
            'ToPort': 443
        }
        ],
        DryRun=True
    )
    return response

Could someone point me to the right direction?有人能指出我正确的方向吗? VPC is non-default. VPC 是非默认的。 All I need is the add ingress rule to existing security group within non-default vpc我所需要的只是将入口规则添加到非默认 vpc 中的现有安全组

Thanks谢谢

Found the solution: Initially it was syntax error but after googling i thought it requires vpc so I added VPC to the Lambda configuration which was not required for this purpose.找到解决方案:最初它是语法错误,但在谷歌搜索后我认为它需要 vpc 所以我将 VPC 添加到 Lambda 配置中,这不是这个目的所必需的。 For anyone having the same issue (only want to update security group with the cidr): below is the correct function and permissions (function isnt complete as depending on the solution u may want to delete old rules too):对于有同样问题的任何人(只想用 cidr 更新安全组):下面是正确的 function 和权限(功能不完整,取决于您可能还想删除旧规则的解决方案):

Lambda function: Lambda function:

#!/usr/bin/python3.9
import boto3
ec2 = boto3.client('ec2')
def lambda_handler(event, context):
    response = ec2.authorize_security_group_ingress(
        DryRun=False,
        GroupId='sg-0123456789',
        IpPermissions=[
            { 
                'FromPort': 443,
                'IpProtocol': 'tcp',
                'IpRanges': [
                    {
                        'CidrIp': '1x.2x.3x.4x/32',
                        'Description': 'Security group updated via lambda'
                    }
                ],
                'ToPort': 443
            }
        ]
    )
    return response

IAM Policy on lambda execution role: IAM Policy on lambda 执行角色:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "ec2:AuthorizeSecurityGroupIngress",
                "ec2:ModifySecurityGroupRules",
                "ec2:UpdateSecurityGroupRuleDescriptionsIngress"
            ],
            "Resource": "arn or all"
        }
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM