@@ -108,24 +108,52 @@ resource "aws_iam_policy_attachment" "dead_letter" {
108108# VPC
109109# #####
110110
111+ // Copying AWS managed policy to be able to attach the same policy with multiple roles without overwrites by another function
112+ data "aws_iam_policy" "vpc" {
113+ count = local. create_role && var. attach_network_policy ? 1 : 0
114+
115+ arn = " arn:aws:iam::aws:policy/service-role/AWSLambdaENIManagementAccess"
116+ }
117+
118+ resource "aws_iam_policy" "vpc" {
119+ count = local. create_role && var. attach_network_policy ? 1 : 0
120+
121+ name = " ${ var . function_name } -vpc"
122+ policy = data. aws_iam_policy . vpc [0 ]. policy
123+ }
124+
111125resource "aws_iam_policy_attachment" "vpc" {
112126 count = local. create_role && var. attach_network_policy ? 1 : 0
113127
114128 name = " ${ var . function_name } -vpc"
115129 roles = [aws_iam_role . lambda [0 ]. name ]
116- policy_arn = " arn:aws:iam::aws:policy/service-role/AWSLambdaENIManagementAccess "
130+ policy_arn = aws_iam_policy . vpc [ 0 ] . arn
117131}
118132
119133# ####################
120134# Tracing with X-Ray
121135# ####################
122136
137+ // Copying AWS managed policy to be able to attach the same policy with multiple roles without overwrites by another function
138+ data "aws_iam_policy" "tracing" {
139+ count = local. create_role && var. attach_tracing_policy ? 1 : 0
140+
141+ arn = " arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess"
142+ }
143+
144+ resource "aws_iam_policy" "tracing" {
145+ count = local. create_role && var. attach_tracing_policy ? 1 : 0
146+
147+ name = " ${ var . function_name } -tracing"
148+ policy = data. aws_iam_policy . tracing [0 ]. policy
149+ }
150+
123151resource "aws_iam_policy_attachment" "tracing" {
124152 count = local. create_role && var. attach_tracing_policy ? 1 : 0
125153
126154 name = " ${ var . function_name } -tracing"
127155 roles = [aws_iam_role . lambda [0 ]. name ]
128- policy_arn = " arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess "
156+ policy_arn = aws_iam_policy . tracing [ 0 ] . arn
129157}
130158
131159# ##############################
0 commit comments