| 34 | } |
| 35 | |
| 36 | pub(crate) async fn create( |
| 37 | deploy: &Deploy, |
| 38 | config: &SdkConfig, |
| 39 | progress: &Progress, |
| 40 | ) -> Result<FunctionRole> { |
| 41 | progress.set_message("creating execution role"); |
| 42 | |
| 43 | let role_name = format!("cargo-lambda-role-{}", uuid::Uuid::new_v4()); |
| 44 | let client = IamClient::new(config); |
| 45 | let sts_client = StsClient::new(config); |
| 46 | let identity = sts_client |
| 47 | .get_caller_identity() |
| 48 | .send() |
| 49 | .await |
| 50 | .into_diagnostic() |
| 51 | .wrap_err("failed to get caller identity")?; |
| 52 | |
| 53 | let mut policy = serde_json::json!({ |
| 54 | "Version": "2012-10-17", |
| 55 | "Statement": [ |
| 56 | { |
| 57 | "Effect": "Allow", |
| 58 | "Action": ["sts:AssumeRole"], |
| 59 | "Principal": { |
| 60 | "Service": "lambda.amazonaws.com" |
| 61 | } |
| 62 | }, |
| 63 | { |
| 64 | "Effect": "Allow", |
| 65 | "Action": ["sts:AssumeRole", "sts:SetSourceIdentity", "sts:TagSession"], |
| 66 | "Principal": { |
| 67 | "AWS": identity.arn().expect("missing account arn"), |
| 68 | } |
| 69 | } |
| 70 | ] |
| 71 | }); |
| 72 | |
| 73 | tracing::trace!(policy = ?policy, "creating role with assume policy"); |
| 74 | |
| 75 | let role = client |
| 76 | .create_role() |
| 77 | .role_name(&role_name) |
| 78 | .assume_role_policy_document(policy.to_string()) |
| 79 | .set_tags(deploy.iam_tags()) |
| 80 | .send() |
| 81 | .await |
| 82 | .into_diagnostic() |
| 83 | .wrap_err("failed to create function role")? |
| 84 | .role |
| 85 | .expect("missing role information"); |
| 86 | |
| 87 | client |
| 88 | .attach_role_policy() |
| 89 | .role_name(&role_name) |
| 90 | .policy_arn(BASIC_LAMBDA_EXECUTION_POLICY) |
| 91 | .send() |
| 92 | .await |
| 93 | .into_diagnostic() |