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