0

I want to create/update the aws policy using boto3

    region= request.POST['env']
    session = get_boto3_session(env=env)
    client = session.client('iam', region_name=region)
    service_name = request.POST['service_name']
    service_value = request.POST['service_vaule']
    group= request.POST['group']  
    policy_name= request.POST['policy_name']
    policyarn = 'arn:aws:iam::'+ region +':policy/'+ policy_name 
    response = client.get_policy(
    PolicyArn=policyarn
    )
    response_json = response['Policy']['DefaultVersionId'] 
    my_managed_policy = json.dumps({
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [],
                "Resource": "*"
            }
        ]
    })
    my_managed_policy['Statement'][0]['Action'].append(
                    #    'Action':[
                    ":".join([service_name,service_value])
                # ],
                    #    'Resource':'*',
                       )
    response = client.create_policy(
        PolicyName=policy_name,
        PolicyDocument=json.dumps(my_managed_policy),
    )
    arn=response["Policy"]["Arn"]
    response = client.attach_group_policy(
        GroupName=pod,
        PolicyArn=arn
    )
    response = client.create_policy_version(
    PolicyArn=arn,
    PolicyDocument=json.dumps(my_managed_policy),
    SetAsDefault=True
    )
    return JsonResponse({"msg": "policy created sucesfully"})  

If i run the code it was not creating new policy showing the error there is no policy exists and if i have created policy manually it was showing the error policy already exists

  • What are the full error messages? Your code is not even a valid python code, thus its not clear what you are doing. – Marcin Sep 29 '22 at 05:56
  • Sorry, but your question (and your code) is difficult to understand. What "ASW policy" are you attempting to update -- is it an IAM policy? What _specific_ problem are you experiencing? Can you please simplify your code to demonstrate this problem, and so we can attempt to reproduce your situation? – John Rotenstein Sep 29 '22 at 08:19
  • I want to update a aws policy using boto3 – Pranisha Pintraj Sep 29 '22 at 11:34

0 Answers0