I'm trying to post my customer name using fetch, in an async function.
async function ExecuteEC2(customer) {
const response = await fetch(
'https:api',
{
method: 'POST',
body: {
Customer: customer,
},
success: function (response) {
alert(response.status);
},
error: function () {
alert("error with database");
}
}
);
}
The function is used in my button so that when it is clicked it triggers the API.
<button
className="btn btn-primary bt-btn btn-size"
type="button"
onClick={() => {
alert("Oh, hi " + customer);
ExecuteEC2(customer);
}}
>
Configure Ansible
</button>
<Modal
showModal={showModal}
setShowModal={setShowModal}
devices={options.AllCSRList}
user={user}
customer={customer}
/>
Finally, the lambda function has been set up to read and apply the API data to our EC2. However, it doesn't show up whenever I try to grab the customer data. Where and how would I amend this to push it through the post?
import time
import json
import boto3
import urllib3
import requests
from pprint import pprint
region = 'eu-west-2'
instance = 'i-ec2'
ec2 = boto3.client('ec2', region_name=region)
def handler(event, context):
customer_name = event['Customer']
print(customer_name)
#boto3 client
client = boto3.client('ec2')
ssm = boto3.client('ssm')
# status = client.describe_instance_status(IncludeAllInstances = True)
# pprint(status)
# response = ssm.send_command(
# InstanceIds=['i-ec2'],
# DocumentName="AWS-RunShellScript",
# Parameters={'commands':[
# "mkdir create2.txt",
# ]},
# )
# print(response)
return {
'statusCode': 200,
'body': json.dumps(customer_name)
}
To fix this issue I had to redo my async function.
I first added the function to my API call file.
async function ExecuteEC2API({ apiName, path, user, body }) {
// replace this with the path you have configured on your API
let jwt = user.signInUserSession.idToken.jwtToken;
let myInit = {
headers: { Authorization: `${jwt}` }, // OPTIONAL
response: true,
queryStringParameters: body,
};
try {
const response = await API.post(apiName, path, myInit);
console.log(await response);
return await response;
} catch (err) {
console.log("fetch failed", err);
}
}
Following this, I then edited the async function in the deploypage.js to call what was needed,
let ExecuteEC2 = async () => {
let response = await ExecuteEC2API({
apiName: ....,
user: user,
body: {
method: "query",
Key: Key,
Customer: customer,
},
});
console.log("EC2", response);
await delay(5);
};
This seemed to do the trick!