Skip to content

Commit ff49f74

Browse files
authored
ci: update example deployment script to use PutResourcePolicy instead of AddPermission (#72)
* chore: update deployment cli to use PutResourcePolicy API * fix: use :* wildcard pattern for resource ARN
1 parent eb96a87 commit ff49f74

File tree

2 files changed

+85
-38
lines changed

2 files changed

+85
-38
lines changed

.github/model/lambda.json

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,26 @@
10551055
],
10561056
"documentation":"<p>Adds a provisioned concurrency configuration to a function's alias or version.</p>"
10571057
},
1058+
"PutResourcePolicy":{
1059+
"name":"PutResourcePolicy",
1060+
"http":{
1061+
"method":"PUT",
1062+
"requestUri":"/2024-09-16/resource-policy/{ResourceArn}",
1063+
"responseCode":200
1064+
},
1065+
"input":{"shape":"PutResourcePolicyRequest"},
1066+
"output":{"shape":"PutResourcePolicyResponse"},
1067+
"errors":[
1068+
{"shape":"InvalidParameterValueException"},
1069+
{"shape":"ResourceConflictException"},
1070+
{"shape":"PublicPolicyException"},
1071+
{"shape":"ServiceException"},
1072+
{"shape":"TooManyRequestsException"},
1073+
{"shape":"PolicyLengthExceededException"},
1074+
{"shape":"ResourceNotFoundException"},
1075+
{"shape":"PreconditionFailedException"}
1076+
]
1077+
},
10581078
"RemoveLayerVersionPermission":{
10591079
"name":"RemoveLayerVersionPermission",
10601080
"http":{
@@ -4737,6 +4757,10 @@
47374757
"type":"integer",
47384758
"min":0
47394759
},
4760+
"NullableBoolean":{
4761+
"type":"boolean",
4762+
"box":true
4763+
},
47404764
"OnFailure":{
47414765
"type":"structure",
47424766
"members":{
@@ -4949,6 +4973,36 @@
49494973
"type":"integer",
49504974
"min":1
49514975
},
4976+
"PolicyResourceArn":{
4977+
"type":"string",
4978+
"max":256,
4979+
"min":0,
4980+
"pattern":"arn:(aws[a-zA-Z-]*)?:lambda:(eusc-)?[a-z]{2}((-gov)|(-iso([a-z]?)))?-[a-z]+-\\d{1}:\\d{12}:(lite-function|function|layer):[a-zA-Z0-9-_]+(:(\\$LATEST|[a-zA-Z0-9-_])+)?"
4981+
},
4982+
"PutResourcePolicyRequest":{
4983+
"type":"structure",
4984+
"required":[
4985+
"ResourceArn",
4986+
"Policy"
4987+
],
4988+
"members":{
4989+
"ResourceArn":{
4990+
"shape":"PolicyResourceArn",
4991+
"location":"uri",
4992+
"locationName":"ResourceArn"
4993+
},
4994+
"Policy":{"shape":"ResourcePolicy"},
4995+
"BlockPublicPolicy":{"shape":"NullableBoolean"},
4996+
"RevisionId":{"shape":"RevisionId"}
4997+
}
4998+
},
4999+
"PutResourcePolicyResponse":{
5000+
"type":"structure",
5001+
"members":{
5002+
"Policy":{"shape":"ResourcePolicy"},
5003+
"RevisionId":{"shape":"RevisionId"}
5004+
}
5005+
},
49525006
"PreconditionFailedException":{
49535007
"type":"structure",
49545008
"members":{
@@ -5410,6 +5464,18 @@
54105464
"error":{"httpStatusCode":404},
54115465
"exception":true
54125466
},
5467+
"ResourcePolicy":{
5468+
"type":"string",
5469+
"max":20480,
5470+
"min":1,
5471+
"pattern":"[\\s\\S]+"
5472+
},
5473+
"RevisionId":{
5474+
"type":"string",
5475+
"max":36,
5476+
"min":36,
5477+
"pattern":"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
5478+
},
54135479
"ResourceNotReadyException":{
54145480
"type":"structure",
54155481
"members":{

examples/cli.py

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -378,45 +378,26 @@ def deploy_function(example_name: str, function_name: str | None = None):
378378
except lambda_client.exceptions.ResourceNotFoundException:
379379
lambda_client.create_function(**function_config, Code={"ZipFile": zip_content})
380380

381-
# Update invoke permission for worker account if needed
382-
try:
383-
policy_response = lambda_client.get_policy(FunctionName=function_name)
384-
policy = json.loads(policy_response["Policy"])
385-
386-
# Check if permission exists with correct principal
387-
needs_update = True
388-
for statement in policy.get("Statement", []):
389-
if (
390-
statement.get("Sid") == "dex-invoke-permission"
391-
and statement.get("Principal", {}).get("AWS")
392-
== config["invoke_account_id"]
393-
):
394-
needs_update = False
395-
break
396-
397-
if needs_update:
398-
with contextlib.suppress(
399-
lambda_client.exceptions.ResourceNotFoundException
400-
):
401-
lambda_client.remove_permission(
402-
FunctionName=function_name, StatementId="dex-invoke-permission"
403-
)
404-
405-
lambda_client.add_permission(
406-
FunctionName=function_name,
407-
StatementId="dex-invoke-permission",
408-
Action="lambda:InvokeFunction",
409-
Principal=config["invoke_account_id"],
410-
)
381+
# Update invoke permission for worker account using put_resource_policy
382+
function_arn = f"arn:aws:lambda:{config['region']}:{config['account_id']}:function:{function_name}"
383+
384+
policy_document = {
385+
"Version": "2012-10-17",
386+
"Statement": [
387+
{
388+
"Sid": "dex-invoke-permission",
389+
"Effect": "Allow",
390+
"Principal": {"AWS": config["invoke_account_id"]},
391+
"Action": "lambda:InvokeFunction",
392+
"Resource": f"{function_arn}:*"
393+
}
394+
]
395+
}
411396

412-
except lambda_client.exceptions.ResourceNotFoundException:
413-
# No policy exists, add permission
414-
lambda_client.add_permission(
415-
FunctionName=function_name,
416-
StatementId="dex-invoke-permission",
417-
Action="lambda:InvokeFunction",
418-
Principal=config["invoke_account_id"],
419-
)
397+
lambda_client.put_resource_policy(
398+
ResourceArn=function_arn,
399+
Policy=json.dumps(policy_document)
400+
)
420401

421402
logger.info("Function deployed successfully! %s", function_name)
422403
return True

0 commit comments

Comments
 (0)