C# AWS KMS: Create Key

Back to Index

Creates a customer master key (CMK) in the caller's AWS account.

Documentation: https://docs.aws.amazon.com/kms/latest/APIReference/API_CreateKey.html

CURL Command

curl -X POST https://kms.us-west-2.amazonaws.com/ \
  -H "Content-Type: application/x-amz-json-1.1" \
  -H "X-Amz-Target: TrentService.CreateKey" \
  -d '{
  "Tags": [{
    "TagValue": "ExampleUser",
    "TagKey": "CreatedBy"
  }]
}'

C# Example

Chilkat.Rest rest = new Chilkat.Rest();
bool success;

Chilkat.AuthAws authAws = new Chilkat.AuthAws();
authAws.AccessKey = "AWS_ACCESS_KEY";
authAws.SecretKey = "AWS_SECRET_KEY";
authAws.Region = "us-west-2";
authAws.ServiceName = "kms";
rest.SetAuthAws(authAws);

//  URL: https://kms.us-west-2.amazonaws.com/
bool bTls = true;
int port = 443;
bool bAutoReconnect = true;
success = rest.Connect("kms.us-west-2.amazonaws.com",port,bTls,bAutoReconnect);
if (success != true) {
    Debug.WriteLine("ConnectFailReason: " + Convert.ToString(rest.ConnectFailReason));
    Debug.WriteLine(rest.LastErrorText);
    return;
}

//  See the Online Tool for Generating JSON Creation Code
Chilkat.JsonObject json = new Chilkat.JsonObject();
json.UpdateString("Tags[0].TagValue","ExampleUser");
json.UpdateString("Tags[0].TagKey","CreatedBy");

rest.AddHeader("Content-Type","application/x-amz-json-1.1");
rest.AddHeader("X-Amz-Target","TrentService.CreateKey");

Chilkat.StringBuilder sbRequestBody = new Chilkat.StringBuilder();
json.EmitSb(sbRequestBody);
Chilkat.StringBuilder sbResponseBody = new Chilkat.StringBuilder();
success = rest.FullRequestSb("POST","/",sbRequestBody,sbResponseBody);
if (success != true) {
    Debug.WriteLine(rest.LastErrorText);
    return;
}

int respStatusCode = rest.ResponseStatusCode;
if (respStatusCode >= 400) {
    Debug.WriteLine("Response Status Code = " + Convert.ToString(respStatusCode));
    Debug.WriteLine("Response Header:");
    Debug.WriteLine(rest.ResponseHeader);
    Debug.WriteLine("Response Body:");
    Debug.WriteLine(sbResponseBody.GetAsString());
    return;
}

Chilkat.JsonObject jsonResponse = new Chilkat.JsonObject();
jsonResponse.LoadSb(sbResponseBody);

//  See the Online Tool for Generating JSON Parse Code

string KeyMetadataAWSAccountId = jsonResponse.StringOf("KeyMetadata.AWSAccountId");
string KeyMetadataArn = jsonResponse.StringOf("KeyMetadata.Arn");
int KeyMetadataCreationDate = jsonResponse.IntOf("KeyMetadata.CreationDate");
string KeyMetadataDescription = jsonResponse.StringOf("KeyMetadata.Description");
bool KeyMetadataEnabled = jsonResponse.BoolOf("KeyMetadata.Enabled");
string KeyMetadataKeyId = jsonResponse.StringOf("KeyMetadata.KeyId");
string KeyMetadataKeyManager = jsonResponse.StringOf("KeyMetadata.KeyManager");
string KeyMetadataKeyState = jsonResponse.StringOf("KeyMetadata.KeyState");
string KeyMetadataKeyUsage = jsonResponse.StringOf("KeyMetadata.KeyUsage");
string KeyMetadataOrigin = jsonResponse.StringOf("KeyMetadata.Origin");

Sample JSON Response Body

{
  "KeyMetadata": {
    "AWSAccountId": "954491899999",
    "Arn": "arn:aws:kms:us-west-2:954491834127:keyabcd6e18-1555-4bca-8450-a2c47abcdb96",
    "CreationDate": 1.526903080724E9,
    "Description": "",
    "Enabled": true,
    "KeyId": "abcd6e18-1555-4bca-8450-abcd79bbabcd",
    "KeyManager": "CUSTOMER",
    "KeyState": "Enabled",
    "KeyUsage": "ENCRYPT_DECRYPT",
    "Origin": "AWS_KMS"
  }
}