apx-aws-lambda 1.2.0__py3-none-any.whl → 1.2.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
apx_aws_lambda/lambda_.py CHANGED
@@ -17,6 +17,7 @@ from action_platform.remote.client import Remote
17
17
  from apx_aws_lambda import shell
18
18
 
19
19
  BOUNDARY = "policy/action-platform/ActionPlatformAppBoundary"
20
+ FAILED_CREATION = ("ROLLBACK_COMPLETE", "ROLLBACK_FAILED")
20
21
  PREFIX_TAG = "action-platform:prefix"
21
22
 
22
23
 
@@ -109,7 +110,7 @@ class LambdaTarget(DeployTarget):
109
110
  prefix = (self.env(ctx) or {}).get("AP_STACK_PREFIX")
110
111
 
111
112
  if prefix:
112
- return f"{prefix}-{'prod' if ctx.stage == 'prod' else 'dev'}"
113
+ return f"{prefix}-{ctx.stage or 'dev'}"
113
114
 
114
115
  params = (
115
116
  self._samconfig(ctx)
@@ -256,10 +257,13 @@ class LambdaTarget(DeployTarget):
256
257
 
257
258
  return checks
258
259
 
259
- def _boundary_parameter(self, ctx: Context) -> Check:
260
+ def _takes_boundary(self, ctx: Context) -> bool:
260
261
  text = (ctx.repo_root / "template.yaml").read_text(errors="replace")
261
262
 
262
- if "PermissionsBoundaryArn" in text and "RolePath" in text:
263
+ return "PermissionsBoundaryArn" in text and "RolePath" in text
264
+
265
+ def _boundary_parameter(self, ctx: Context) -> Check:
266
+ if self._takes_boundary(ctx):
263
267
  return Check(
264
268
  "template.boundary", True, "the execution role takes the boundary"
265
269
  )
@@ -277,7 +281,7 @@ class LambdaTarget(DeployTarget):
277
281
  if status is None:
278
282
  return Check("stack.state", True, "no stack yet; the deploy creates it")
279
283
 
280
- if status == "ROLLBACK_COMPLETE":
284
+ if status in FAILED_CREATION:
281
285
  return Check(
282
286
  "stack.state",
283
287
  True,
@@ -430,38 +434,77 @@ class LambdaTarget(DeployTarget):
430
434
  return rows[0].get("StackStatus") if rows else None
431
435
 
432
436
  def _clear_failed_creation(self, ctx: Context) -> None:
433
- """A stack whose first creation failed sits in ROLLBACK_COMPLETE and refuses updates; it never existed, so it is deleted before the deploy creates it again."""
434
- if self._status(ctx) != "ROLLBACK_COMPLETE":
437
+ """A stack whose first creation failed sits in ROLLBACK_COMPLETE — or ROLLBACK_FAILED when a resource could not be rolled back — and refuses updates; it never existed, so it is deleted before the deploy creates it again."""
438
+ status = self._status(ctx)
439
+
440
+ if status not in FAILED_CREATION:
435
441
  return
436
442
 
437
443
  stack = self._stack(ctx)
438
- logger.info(
439
- "stack %s is ROLLBACK_COMPLETE: deleting it before the deploy", stack
440
- )
444
+ logger.info("stack %s is %s: deleting it before the deploy", stack, status)
441
445
  region = self._region(ctx)
442
446
  env = self.env(ctx)
443
- shell.aws(
444
- "cloudformation",
445
- "delete-stack",
446
- "--stack-name",
447
- stack,
448
- region=region,
449
- env=env,
450
- )
451
- shell.aws(
452
- "cloudformation",
453
- "wait",
454
- "stack-delete-complete",
455
- "--stack-name",
456
- stack,
457
- region=region,
458
- env=env,
459
- )
447
+ self._delete_stack(stack, region, env)
448
+
449
+ if self._status(ctx) != "DELETE_FAILED":
450
+ return
451
+
452
+ stuck = [
453
+ row["LogicalResourceId"]
454
+ for row in shell.aws(
455
+ "cloudformation",
456
+ "describe-stack-resources",
457
+ "--stack-name",
458
+ stack,
459
+ region=region,
460
+ env=env,
461
+ ).get("StackResources", [])
462
+ if row.get("ResourceStatus") == "DELETE_FAILED"
463
+ ]
464
+ logger.info("stack %s kept %s: deleting it without them", stack, stuck)
465
+ self._delete_stack(stack, region, env, retain=stuck)
466
+
467
+ def _delete_stack(
468
+ self,
469
+ stack: str,
470
+ region: str | None,
471
+ env: dict[str, str] | None,
472
+ retain: list[str] | None = None,
473
+ ) -> None:
474
+ """Delete and wait; a delete that fails leaves the stack in DELETE_FAILED for the caller to look at."""
475
+ args = ["cloudformation", "delete-stack", "--stack-name", stack]
476
+
477
+ if retain:
478
+ args += ["--retain-resources", *retain]
479
+
480
+ shell.aws(*args, region=region, env=env)
481
+
482
+ try:
483
+ shell.aws(
484
+ "cloudformation",
485
+ "wait",
486
+ "stack-delete-complete",
487
+ "--stack-name",
488
+ stack,
489
+ region=region,
490
+ env=env,
491
+ )
492
+ except DeployError:
493
+ if retain:
494
+ raise
460
495
 
461
496
  def deploy(self, ctx: Context) -> DeployResult:
462
497
  sam = shell.require("sam", "pip install aws-sam-cli")
463
498
  stage = self._stage(ctx)
464
499
  env = self.env(ctx)
500
+
501
+ if (env or {}).get("AP_BOUNDARY") and not self._takes_boundary(ctx):
502
+ raise DeployError(
503
+ "template.yaml lacks the PermissionsBoundaryArn or RolePath parameter, so the execution role "
504
+ "would land outside what a connected account may create: apply the aws/lambda overlay again "
505
+ "(Configuration → Deploy target, or action-platform cloud set aws/lambda)"
506
+ )
507
+
465
508
  shell.run([*sam, "build"], cwd=ctx.repo_root, env=env)
466
509
  self._clear_failed_creation(ctx)
467
510
  args = [
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: apx-aws-lambda
3
- Version: 1.2.0
3
+ Version: 1.2.2
4
4
  Summary: AWS Lambda for Action Platform: the aws/lambda deploy target (SAM) with its overlay, and tools to read stacks and functions.
5
5
  Author-email: Action Platform <cloud@actionplatform.io>
6
6
  License-Expression: Apache-2.0
@@ -93,7 +93,7 @@ What the stack creates:
93
93
  | `ActionPlatformAppBoundary` (`/action-platform/`) | the cap on every execution role an app's stack creates — logs `/aws/lambda/<prefix>*`; S3 buckets, DynamoDB tables, SQS queues, SNS topics, Lambda functions `<prefix>-*`; Secrets Manager secrets and SSM parameters `<prefix>/*`; X-Ray — `<prefix>` being the role's `action-platform:prefix` tag. An app's own `template.yaml` still declares what its function needs; the boundary caps it |
94
94
  | `ActionPlatformDeploy` role (`/action-platform/`) | trust: `AssumeRoleWithWebIdentity` for `sub` `org:<org>:*`, and `TagSession` only for the one tag `action-platform:prefix` with a value `ap-<org>-*`. Policy: every resource under `${aws:PrincipalTag/action-platform:prefix}` — CloudFormation stacks, Lambda, logs; API Gateway; the public Lambda Web Adapter layers; SAM's bucket; `iam:CreateRole` on `<prefix>-*` only with the boundary attached and the app's own prefix tag, `iam:PassRole` of those roles to Lambda only; `iam:SimulatePrincipalPolicy` on itself |
95
95
 
96
- How a deploy runs: the platform signs a token for the app with the claim `https://aws.amazon.com/tags` → `principal_tags.action-platform:prefix = ap-<org>-<project>-<app>`; the target assumes the deploy role with it, names the stack `ap-<org>-<project>-<app>-dev|prod`, passes `PermissionsBoundaryArn` to the overlay's template and tags the stack with the prefix, so the execution role SAM creates carries both the boundary and the tag. `sam delete` removes that role with the stack. Who may deploy which app is the platform's decision — it only signs a token for an app the job or the caller is for.
96
+ How a deploy runs: the platform signs a token for the app with the claim `https://aws.amazon.com/tags` → `principal_tags.action-platform:prefix = ap-<org>-<project>-<app>`; the target assumes the deploy role with it, names the stack `ap-<org>-<project>-<app>-<scope>`, passes `PermissionsBoundaryArn` to the overlay's template and tags the stack with the prefix, so the execution role SAM creates carries both the boundary and the tag. `sam delete` removes that role with the stack. Who may deploy which app is the platform's decision — it only signs a token for an app the job or the caller is for.
97
97
 
98
98
  Readiness simulates the deploy role with the session tag and the boundary as context entries, and flags an overlay without the `PermissionsBoundaryArn` parameter (apply the overlay again). The stack's policy is the one place to widen or narrow what deploys may do: edit and update the stack.
99
99
 
@@ -1,6 +1,6 @@
1
1
  apx_aws_lambda/__init__.py,sha256=Q0-IuA8uSNSj-dfiZsLHG509XAkVn9y5IMdbXdgGsMw,212
2
2
  apx_aws_lambda/cli.py,sha256=KsrnF3rGb_oTSY0Ke_XKzUghtPmXA0Pm9D4XtYiXPZw,3458
3
- apx_aws_lambda/lambda_.py,sha256=8DjveGR0DkXWqEabKh9rGg7x_bIdk_yVsi9zvwDfpfA,22656
3
+ apx_aws_lambda/lambda_.py,sha256=W-Z7bl_NLKo0dauNeOICtDe71LRzlCvhDf--1YGnl_k,24358
4
4
  apx_aws_lambda/plugin.py,sha256=7PuZ2BlSN0_aRk5jvmbpUAXExfLXEbl-s1ivSOrFt1s,2445
5
5
  apx_aws_lambda/shell.py,sha256=5Er0u92dD6r7Nb8bKGTBsniXPASCrn5cLAlIAPWfP7k,2531
6
6
  apx_aws_lambda/tools.py,sha256=i1vSfBslaiw8wUhgTctI_V4FHYSDiaGMDhMe0JxLs1g,2322
@@ -15,9 +15,9 @@ apx_aws_lambda/overlays/cloud/aws/lambda/{{cookiecutter.project_slug}}/template.
15
15
  apx_aws_lambda/overlays/cloud/aws/lambda/{{cookiecutter.project_slug}}/.github/workflows/deploy.yml,sha256=3q3R4zWAA4xO3bbaotrOI-b6MjaaVjhpSJcDCyj_0lg,2244
16
16
  apx_aws_lambda/overlays/cloud/aws/lambda/{{cookiecutter.project_slug}}/requirements/policy.json,sha256=yow1_lHYADDu1bn8SJMCqXptuVsWCJ31gtc9iqMnnbI,2657
17
17
  apx_aws_lambda/overlays/cloud/aws/lambda/{{cookiecutter.project_slug}}/requirements/trust.json,sha256=odQnKozS_9c2v28aOBocBSuVuowb3_xRe3ZGSKFCHcM,564
18
- apx_aws_lambda-1.2.0.dist-info/licenses/LICENSE,sha256=WcDWtpGI54c3iNg7UddKYdRlt9oUzbMjUzdgKqsE8SI,11263
19
- apx_aws_lambda-1.2.0.dist-info/METADATA,sha256=cp-wNGm73cnDb3kfJr4qruEoReeX5kXOCbOD2Aw5JRQ,11049
20
- apx_aws_lambda-1.2.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
21
- apx_aws_lambda-1.2.0.dist-info/entry_points.txt,sha256=3uAwnYt-VgVll4NPMHp0r36oxHXeivvg1N8w5s5x43I,152
22
- apx_aws_lambda-1.2.0.dist-info/top_level.txt,sha256=hAm2-VyixplJdw19PaHTNxZzi9x3yL3lXgsKbVUvgLg,15
23
- apx_aws_lambda-1.2.0.dist-info/RECORD,,
18
+ apx_aws_lambda-1.2.2.dist-info/licenses/LICENSE,sha256=WcDWtpGI54c3iNg7UddKYdRlt9oUzbMjUzdgKqsE8SI,11263
19
+ apx_aws_lambda-1.2.2.dist-info/METADATA,sha256=usDpqpTMiR800hco3RsJwhMiO2DwIfqu9OJNKZYxqJ0,11048
20
+ apx_aws_lambda-1.2.2.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
21
+ apx_aws_lambda-1.2.2.dist-info/entry_points.txt,sha256=3uAwnYt-VgVll4NPMHp0r36oxHXeivvg1N8w5s5x43I,152
22
+ apx_aws_lambda-1.2.2.dist-info/top_level.txt,sha256=hAm2-VyixplJdw19PaHTNxZzi9x3yL3lXgsKbVUvgLg,15
23
+ apx_aws_lambda-1.2.2.dist-info/RECORD,,